Need to source .bash_profile on target system

6,288 views
Skip to first unread message

bob....@evotext.com

unread,
Mar 24, 2015, 1:51:59 PM3/24/15
to ansible...@googlegroups.com
Hi,

I'm new to ansible and I'm trying to get ansible to source the .bash_profile of the user on the target system.  We set many environment variables there in order to run a multitude of scripts.  I know that ansible doesn't support this.  Any thoughts on how I can force ansible to source this file?

Thanks!

bob

Anand Sharma

unread,
Apr 13, 2015, 8:12:21 AM4/13/15
to ansible...@googlegroups.com
Just curious: Were you able to figure a way around this??

Ivan S. Freitas

unread,
Apr 13, 2015, 10:48:26 AM4/13/15
to ansible...@googlegroups.com
Once I had to do something similar and the workaround I used was a
command action calling bash with the -l option, e.g.:

$ ansible server -m command -a "bash -l -c 'env'"

Will print all existent variables, and the -l (makes bash start as a
login shell) forces it to read the .bash_profile (and I also think it
forces reading the .bashrc file too).

I ended moving all environment variables to files in /etc/profile.d
that are sourced by the shell even without the -l or -i options.

On Sun, Apr 12, 2015 at 9:23 PM, Anand Sharma <anand....@gmail.com> wrote:
> Just curious: Were you able to figure a way around this??
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To post to this group, send email to ansible...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/59139a35-53a9-4bb5-9cd0-f04ce3c2f294%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Ivan Sichmann Freitas

Mona Gopal

unread,
Jan 11, 2017, 12:59:27 AM1/11/17
to Ansible Project
Hi,

Even i am trying to source the bashrc file on my target system.
Ansible runs successfully without errors but doesn't source the file.
Below is my task:

- name: source bashrc file
  shell: source ~/.bashrc
  args:
     executable: /bin/bash

Didn't really find that executable arg helping me out.
Any other way around??
Kindly help.

Thanks in advance
Mona G

Andreas Olsson

unread,
Jan 11, 2017, 1:59:55 AM1/11/17
to ansible...@googlegroups.com
tis 2017-01-10 klockan 21:59 -0800 skrev Mona Gopal:
> Even i am trying to source the bashrc file on my target system.
> Ansible runs successfully without errors but doesn't source the file.
> Below is my task:
>
> - name: source bashrc file
>   shell: source ~/.bashrc
>   args:
>      executable: /bin/bash

The problem here isn't that Ansible isn't sourcing the file. The
problem is that Ansible isn't sourcing it in a helpful way.

Defined environment variables only take affect in the current process
and its child processes. It doesn't have any affect on parent nor
sibling processes.

Hence, what is happening here is that Ansible is starting a new shell
processes which sources your ~/.bashrc. Right afterwards the shell
processes is exited, and Ansible forgets all about its environment.

Is it an alternative to instead explicitly define the needed
environment variables in the playbook?

  https://docs.ansible.com/ansible/playbooks_environment.html

// Andreas
Reply all
Reply to author
Forward
0 new messages