Unable to run bashrc file using ansible - ansible

Unable to run bashrc file using ansible.
- name: Source Bashrc
action: shell source /local/apps/actional/.bash_profile
is not working.

source is a build-in, not command.
Try this:
---
- hosts: target-server
gather_facts: no
tasks:
- copy:
content: export MYVAR=123
dest: /tmp/source_test
- shell: /bin/bash -c 'source /tmp/source_test; echo $MYVAR'
Keep in mind that you can use sourced environment only within one Ansible task – each task is executed in new shell.

Related

Ansible become user do not run /etc/profile

I have playbook that installs shell scripts into /etc/profile.d/app.sh which appends my application start and stop scripts to $PATH, These 2 below steps runs as root user
- name: add binaries to PATH
copy:
args:
src: app.sh
dest: /etc/profile.d/
owner: root
group: root
mode: 0644
Then I am sourcing /etc/profile to reflect the changes
- name: reload profile
shell: source /etc/profile
After this I need to switch to my application user "appuser" to start the process
- name: start the application
shell: startMyapp application
become: yes
become_user: appuser
But I get error startMyapp: command not found, looks like when I use become_user /etc/profile is not getting executed to get the updated $PATH.
How can I make changes in my playbook that source /etc/profile when I use become_user?

cannot echo a variable to remote bashrc via ansible

Somewhere I am not being able to read / write to ~/.bashrc in remote hosts with Ansible.
I tried, previously with .bashrc and now with .profile but all in vain :
- name: install elasticsearch
command: "{{ item }}"
with_items:
- "cd /apps/dmg/ && tar -xzf elasticsearch-6.4.3.tar.gz"
- "cd /apps/dmg/elasticsearch-6.4.3"
#- echo "ES_HOME=/apps/dmg/elasticsearch-6.4.3" >> ~/.profile
#- "source ~/.profile"
I realised that, ansible task just isnt writing to any file in there at remote hosts, because no "~/.profile" file exists even after running this task, which apparently succeeds :
changed: [WONTTELLXXXX] => (item=echo "ES_HOME=/apps/dmg/elasticsearch-6.4.3" >> ~/.profile)
Do not use the expansion ~ (citation needed wip)
~/.profile
Use full path for remote_user, become_user, or any other if needed. For example
"/home/{{ remote_user }}/.profile"
$HOME might work too
"$HOME/.profile"
Notes
The tasks below would help to find out what is going on
- command: whoami
register: result
- debug: var=result.stdout
- command: pwd
register: result
args:
chdir: $HOME
- debug: var=result.stdout
- command: pwd
register: result
args:
chdir: ~
- debug: var=result.stdout

Ansible, copy script, then execute in remote machine

I want to copy a script to a remote server and then execute it. I can copy it to the directory /home/user/scripts, but when I run ansible script, it returns Could not find or access '/home/user/scripts/servicios.sh'
The full error output is:
fatal: [192.168.1.142]: FAILED! => {"changed": false, "msg": "Could not find or access '/home/user/scripts/servicios.sh'"}
Here is the ansible playbook
- name: correr script
hosts: all
tasks:
- name: crear carpeta de scripts
file:
path: /home/user/scripts
state: directory
- name: copiar el script
copy:
src: /home/local/servicios.sh
dest: /home/user/scripts/servicios.sh
- name: ejecutar script como sudo
become: yes
script: /home/user/scripts/servicios.sh
You don’t need to create a directory and copy the script to target (remote node), the script module does that for you. It takes the script name followed by a list of space-delimited arguments. The local script at path will be transferred to the remote node and then executed. The script will be processed through the shell environment on the remote node. You were getting the error because script module expects the path /home/user/scripts/servicios.sh on your Ansible controller (the node where you are running the playbook from). To make it work you can specify correct path (/home/local/servicios.sh) in script task instead of /home/user/scripts/servicios.sh which is the path on the remote node. So you can change the playbook like this: You can also register the result of that command as a variable if you would like to see that.
---
- name: correr script
hosts: all
become: yes
tasks:
- name: ejecutar script como sudo
script: /home/local/servicios.sh
register: console
- debug: msg="{{ console.stdout }}"
- debug: msg="{{ console.stderr }}"
What if don’t want to go for script module and you are interested in creating a directory and copy the script to target (remote node) explicitly, and run it? No worries, you can still use the command module like this:
---
- name: correr script
hosts: all
become: yes
tasks:
- name: crear carpeta de scripts
file:
path: /home/user/scripts
state: directory
- name: copiar el script
copy:
src: /home/local/servicios.sh
dest: /home/user/scripts/servicios.sh
- name: ejecutar script como sudo
command: bash /home/user/scripts/servicios.sh
register: console
- debug: msg="{{ console.stdout }}"
- debug: msg="{{ console.stderr }}"
But I strongly recommend to go for script module.
The script tag itself transfers the script from the local machine to remote machine and executes it there.
So, the path specified in the script module is of the local machine and not the remote machine i.e., /home/local/servicios.sh instead of /home/user/scripts/servicios.sh
As you have specified the path which is supposed to be on the remote machine, ansible is unable to find that script on local machine at the given path which results in the given error.
Hence, update the path in the task to local path as shown below,
- name: ejecutar script como sudo
become: yes
script: /home/local/servicios.sh
So scripts cant be executed inside the remote server and should be
executed via local machine to the remote?
#thrash3d No, it is not like that. When you use script tag the script is transferred to the remote machine and then it is executed there. If there is a script which you don't want to put on your remote machine and just want to execute it then you can use script tag.
If you want that script on your remote machine then you can first copy your script on remote machine and then execute it there.
Both ways are correct and it is up to you which case suits you better.

File created by ansible script module not present after running against remote node

I'm setting up some server in AWS, and want to use Ansible to do some shell in remote nodes. I write playbook as follow
- hosts: remote-nodes
tasks:
- name: Execute script
script: /home/ubuntu/FastBFT_ethereum/experiment/a.sh
remote nodes a.sh as follow
#!/usr/bin/env bash
echo "test">> test.txt
python writejson.py
But when I check the test.text, I find it doesn't work in remote nodes.help me please.
Assuming that you want test.txt to be created in the experiment directory, this should be changed to something like:
- hosts: remote-nodes
tasks:
- name: Execute script
script: /home/ubuntu/FastBFT_ethereum/experiment/a.sh
args:
chdir: /home/ubuntu/FastBFT_ethereum/experiment

Setting an environment variable in Ansible from a command output of bash command

I would like to set output of a shell command as an environment variable in Ansible.
I did the following to achieve it:
- name: Copy content of config.json into variable
shell: /bin/bash -l -c "cat /storage/config.json"
register: copy_config
tags: something
- name: set config
shell: "echo $TEMP_CONFIG"
environment:
TEMP_CONFIG: "{{copy_config}}"
tags: something
But somehow after the ansible run, when I do run the following command:
echo ${TEMP_CONFIG}
in my terminal it gives an empty result.
Any help would be appreciated.
There are at least two problems:
You should pass copy_config.stdout as a variable
- name: set config
shell: "echo $TEMP_CONFIG"
environment:
TEMP_CONFIG: "{{copy_config.stdout}}"
tags: something
You need to register the results of the above task and then again print the stdout, so:
- name: set config
shell: "echo $TEMP_CONFIG"
environment:
TEMP_CONFIG: "{{copy_config.stdout}}"
tags: something
register: shell_echo
- debug:
var: shell_echo.stdout
You never will be able to pass the variable to a non-related process this way. So unless you registered the results in an rc-file (like ~/.bash_profile which is sourced on interactive login if you use Bash) no other shell process would be able to see the value of TEMP_CONFIG. This is how system works.

Resources