# Which of .bashrc, .profile, .bash\_profile, .bash\_login or .login is used?

**URL:** <https://forum.ansible.com/t/which-of-bashrc-profile-bash-profile-bash-login-or-login-is-used/22582>\
**Category:** Ansible Project\
**Created:** [September 19, 2016, 7:37am UTC](https://forum.ansible.com/t/which-of-bashrc-profile-bash-profile-bash-login-or-login-is-used/22582 "2016-09-19T07:37:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harald\_Kubota](https://avatars.discourse-cdn.com/v4/letter/h/77aa72/32.png) [@Harald\_Kubota](https://forum.ansible.com/u/Harald_Kubota)\
**Post date:** [September 19, 2016, 7:37am UTC](https://forum.ansible.com/t/which-of-bashrc-profile-bash-profile-bash-login-or-login-is-used/22582/1 "2016-09-19T07:37:07Z")

</div>

Hi,

Still learning Ansible, so if this is a stupid question, let me know.  
I’d like to know which (if any) of the typical shell init files are used when executing commands via the shell module. Specifically I’d like to run node.  
This works interactively just fine:

harald@giga:~/vagrantstuff/node/ansible$ ssh node3

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 19, 2016, 6:09pm UTC](https://forum.ansible.com/t/which-of-bashrc-profile-bash-profile-bash-login-or-login-is-used/22582/2 "2016-09-19T18:09:10Z")

</div>

> Still learning Ansible, so if this is a stupid question, let me know.  
> I'd like to know which (if any) of the typical shell init files are used  
> when executing commands via the shell module. Specifically I'd like to run  
> node.  
> This works interactively just fine:
> 
> harald@giga:~/vagrantstuff/node/ansible$ ssh node3  
> ----------------------------------------------------------------  
> Debian GNU/Linux 8.5 (jessie) built 2016-08-28  
> ----------------------------------------------------------------  
> Last login: Mon Sep 19 07:11:18 2016 from giga.lan  
> harald@node3:~$ node --version  
> v6.6.0  
> harald@node3:~$ echo $PATH  
> /home/harald/node:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games  
> harald@node3:~$ echo $SHELL  
> /bin/bash  
> harald@node3:~$ echo $LAST  
> bash\_profile
> 
> PATH is set to include ~/node if it exists. I put this usually into  
> .bashrc, but that's not working when using Ansible.  
> And I spend half of today to find out why.
> 
> To find out what of those files are used at all I set LAST in all of  
> ~/.profile, ~/.login, ~/.bash\_profile, ~/.bash\_login and ~/.bashrc to match  
> the profile file name it's in.  
> E.g. in .bashrc at the bottom:
> 
> LAST=bashrc  
> export LAST
> 
> The rather simple playbook:
> 
> - hosts: nodes  
> &nbsp;&nbsp;gather\_facts: false  
> &nbsp;&nbsp;tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Testing to run node  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;shell: echo "SHELL=$SHELL" ; echo "PATH=$PATH" ; echo "LAST=$LAST";  
> node --version
> 
> however does not seem to honor any of those files:
> 
> fatal: [node3]: FAILED! =\> {"changed": true, "cmd": "echo \"SHELL=$SHELL\"  
> ; echo \"PATH=$PATH\"  
> ; echo \"LAST=$LAST\"; node --version", "delta": "0:00:00.009935", "end":  
> "2016-09-19 07:24:49.  
> 008019", "failed": true, "rc": 127, "start": "2016-09-19 07:24:48.998084",  
> "stderr": "/bin/bash:  
> node: command not found", "stdout":  
> "SHELL=/bin/bash\nPATH=/usr/local/bin:/usr/bin:/bin:/usr/ga  
> mes\nLAST=", "stdout\_lines": ["SHELL=/bin/bash",  
> "PATH=/usr/local/bin:/usr/bin:/bin:/usr/games",  
> "LAST="], "warnings": }
> 
> As you can see /bin/bash is used (my default shell). But LAST is not set  
> and PATH is not set either which makes the "node" command fail.

Ansible shell module is using /bin/sh by default. In Debian /bin/sh is a symlink to dash. Dash do read /etc/profile and $HOME/.profile, but with Ansible shell those files is not read at all in my experience.

> Note that I run ansible-playbook as myself, so there should be no changing  
> of accounts.  
> If it's relevant: I use Ansible 2.1.1.0
> 
> What am I missing?

/etc/pam.d/sshd is set to read the environment from /etc/environment and changes here will be available in Ansible.

I'm using Kubuntu so I can't confirm it's working in Debian.

---

<div class="post-metadata">

**Author:** ![Harald\_Kubota](https://avatars.discourse-cdn.com/v4/letter/h/77aa72/32.png) [@Harald\_Kubota](https://forum.ansible.com/u/Harald_Kubota)\
**Post date:** [September 22, 2016, 9:38am UTC](https://forum.ansible.com/t/which-of-bashrc-profile-bash-profile-bash-login-or-login-is-used/22582/3 "2016-09-22T09:38:50Z")

</div>

Thanks Kai for the reply. Unfortunately it does not solve the problem. When using /bin/sh which is a link to /bin/dash, or when using /bin/bash, it seems that none of the user’s profile files is used.  
I cannot modify /etc/pam.d/\* or /etc/ssh/sshd\* because everything works just fine when using ssh by itself as the same user I use for ansible.

If it’s up to me, this is a bug as I find no reason why it should behave like it does when I run ansible as a normal user.

Harald

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [September 22, 2016, 11:17am UTC](https://forum.ansible.com/t/which-of-bashrc-profile-bash-profile-bash-login-or-login-is-used/22582/4 "2016-09-22T11:17:20Z")

</div>

> Thanks Kai for the reply. Unfortunately it does not solve the problem. When  
> using /bin/sh which is a link to /bin/dash, or when using /bin/bash, it  
> seems that none of the user's profile files is used.

A shell do distinguish between a login mode and a interactive mode.

When you manual log in with ssh, you get a login shell, and a dash login shell do read /etc/profile and .profile.

When Ansible start a shell its not a login shell, but a interactive dash shell do not read /etc/profile and .profile.  
It read the environment ENV if it exist.

You can read more about this in "man dash" and "man bash", search for "Invocation".

> I cannot modify /etc/pam.d/\* or /etc/ssh/sshd\* because everything works  
> just fine when using ssh by itself as the same user I use for ansible.

I didn't suggest you change any of this files.  
I tried to explain that some distribution do use files in /etc/pam.d to inject parameters in some situations.

And that in Kubuntu this is the file /etc/environment, and since it based on Debian it's probably the same file for you, but I can't confirm it since I do not have Debian 8.5.

> If it's up to me, this is a bug as I find no reason why it should behave  
> like it does when I run ansible as a normal user.

It has nothing to do with Ansible, its just how the shells work.

---

<div class="post-metadata">

**Author:** ![rup](https://avatars.discourse-cdn.com/v4/letter/r/71c47a/32.png) [@rup](https://forum.ansible.com/u/rup)\
**Post date:** [September 22, 2016, 4:26pm UTC](https://forum.ansible.com/t/which-of-bashrc-profile-bash-profile-bash-login-or-login-is-used/22582/5 "2016-09-22T16:26:07Z")

</div>

> I ran into the same issue and spent many hours trying to find out why it wasn’t using the profiles. I like your LAST test to see which profile was being used, very clever.

I solved it by simply sourcing each profile I needed prior to the step I wanted to execute. For example:

- name: Run some step  
shell: . /pathtobashrc/.bashrc && . /pathtoprofile/profile && step I wanted to execute

---

<div class="post-metadata">

**Author:** ![Harald\_Kubota](https://avatars.discourse-cdn.com/v4/letter/h/77aa72/32.png) [@Harald\_Kubota](https://forum.ansible.com/u/Harald_Kubota)\
**Post date:** [September 22, 2016, 9:10pm UTC](https://forum.ansible.com/t/which-of-bashrc-profile-bash-profile-bash-login-or-login-is-used/22582/6 "2016-09-22T21:10:20Z")

</div>

Thanks Kai.

The explanation is very much appreciated as it sheds some light into this (for me) unexpected behavior.

Harald

---

<div class="post-metadata">

**Author:** ![Harald\_Kubota](https://avatars.discourse-cdn.com/v4/letter/h/77aa72/32.png) [@Harald\_Kubota](https://forum.ansible.com/u/Harald_Kubota)\
**Post date:** [September 22, 2016, 9:16pm UTC](https://forum.ansible.com/t/which-of-bashrc-profile-bash-profile-bash-login-or-login-is-used/22582/7 "2016-09-22T21:16:28Z")

</div>

I guess there is no other way. Maybe I just take it as a nudge to try to use more Ansible modules and less shell scripts.

Harald
