Ansible doesn't load ~/.profile

Ansible is not running tasks in an interactive shell on the remote host. Michael DeHaan has answered this question on github some time ago:

The uber-basic description is ansible isn't really doing things through the shell, it's transferring modules and executing scripts that it transfers, not using a login shell.

i.e. Why does an SSH remote command get fewer environment variables then when run manually?

It's not a continous shell environment basically, nor is it logging in and typing commands and things.

You should see the same result (undefined variable) by running this:

ssh <host> echo $ENV_VAR

In a lot of places I've used below structure:

- name: Task Name
  shell: ". /path/to/profile;command"

Ansible is not running remote tasks (command, shell, ...) in an interactive nor login shell. It's same like when you execute command remotely via 'ssh user@host "which python"' To source ~/.bashrc won't work often because ansible shell is not interactive and ~/.bashrc implementation by default ignores non interactive shell (check its beginning).

The best solution for executing commands as user after its ssh interactive login I found is:

- hosts: all
  tasks:
    - name: source user profile file
      #become: yes
      #become_user: my_user  # in case you want to become different user (make sure acl package is installed)
      shell: bash -ilc 'which python' # example command which prints
      register: which_python
    - debug:
      var: which_python

bash: '-i' means interactive shell, so .bashrc won't be ignored '-l' means login shell which sources full user profile (/etc/profile and ~/.bash_profile, or ~/.profile - see bash manual page for more details)

Explanation of my example: my ~/.bashrc sets specific python from anaconda installed under that user.

Tags:

Ansible