I can ssh to the remote host and do a source /home/username/.bashrc
- everything works fine.
However if I do:
- name: source bashrc
sudo: no
The right way should be:
- hosts: all
tasks:
- name: source bashrc file
shell: "{{ item }}"
with_items:
- source ~/.bashrc
- your other command
Note: it's test in ansible 2.0.2
version
So command
will only run executables. source
per se is not an executable. (It's a builtin shell command).
Is there any reason why you want to source
a full environment variable?
There are other ways to include environment variables in Ansible. For example, the environment
directive:
- name: My Great Playbook
hosts: all
tasks:
- name: Run my command
sudo: no
action: command <your-command>
environment:
HOME: /home/myhome
Another way is to use the shell
Ansible module:
- name: source bashrc
sudo: no
action: shell source /home/username/.bashrc && <your-command>
or
- name: source bashrc
sudo: no
shell: source /home/username/.bashrc && <your-command>
In these cases, the shell instance/environment will terminate once the Ansible step is run.
Well I tried the listed answers but those didn't worked for me while installing ruby through rbenv. I had to source below lines from /root/.bash_profile
PATH=$PATH:$HOME/bin:$HOME/.rbenv/bin:$HOME/.rbenv/plugins/ruby-build/bin
export PATH
eval "$(rbenv init -)"
Finally, I came up with this
- shell: sudo su - root -c 'rbenv install -v {{ ruby_version }}'
One can use this with any command.
- shell: sudo su - root -c 'your command'
My 2 cents, i circumnavigated the problem sourcing ~/.nvm/nvm.sh
into ~/.profile
and then using sudo -iu
as suggested in another answer.
Tried on January 2018 vs Ubuntu 16.04.5
- name: Installing Nvm
shell: >
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
args:
creates: "/home/{{ ansible_user }}/.nvm/nvm.sh"
tags:
- nodejs
- name: Source nvm in ~/.profile
sudo: yes
sudo_user: "{{ ansible_user }}"
lineinfile: >
dest=~/.profile
line="source ~/.nvm/nvm.sh"
create=yes
tags:
- nodejs
register: output
- name: Installing node
command: sudo -iu {{ ansible_user }} nvm install --lts
args:
executable: /bin/bash
tags:
- nodejs