0

I am encountering a problem where I am running the same tasks on 2 remote nodes and the directories that those commands are executed at are different.

If I run pwd through Ansible on each remote host before this command, they return different paths. For example /usr and /usr/src. If I log into the remote host manually I go to /usr/src for both (As specified in their configuration files).

Can anyone explain to me why is this happening? To what directory does Ansible go if you run a command without specifying a chdir?

moonmoon
  • 110
  • 10

1 Answers1

1

I would expect this difference to happen because, when login in manually, you have a .bashrc that cd you in the right folder in one of those two hosts, when Ansible does not source the .bashrc file.

Per default, ssh, and, so, Ansible, logs you into the $HOME folder of the user you define Ansible to connect with, which you can also find in /etc/passwd

Another reason I could see for this to happen would be because you use one user to log into the node but then become another one.

inventory.yml

all:
  hosts:
    some.example.com:
      ansible_user: some_user

playbook.yml

---
- hosts: all
  tasks:

  - command: pwd # still you will be in /home/some_user
    become: yes
    become_user: some_other_user
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • " ~/.bash_profile is executed for login shells and ~/.bashrc is executed for interactive non-login shells. " https://attack.mitre.org/techniques/T1156/ That would mean it is actually executing bashrc – moonmoon Jan 24 '20 at 11:32
  • No, neither one neither the other. Further reading: https://github.com/ansible/ansible/issues/29637#issuecomment-380672737. Remember that Ansible doesn’t do pure SSH, it uses a python module that connects you to your hosts (and that connection is even configurable). So your safer bet is either to set a the folder as home to your users, either to `chdir`, and the later is your safest bet. – β.εηοιτ.βε Jan 24 '20 at 17:57
  • And if I remember properly, per default, Ansible uses the paramiko plugin to connect https://docs.ansible.com/ansible/latest/plugins/connection.html – β.εηοιτ.βε Jan 24 '20 at 18:05