Ansible doesn't load ~/.profile

Viewed 12626

I'm asking myself why Ansible doesn't source ~/.profile file before execute template module on one host ?

Distant host ~/.profile:

export ENV_VAR=/usr/users/toto

A single Ansible task:

- template: src=file1.template dest={{ ansible_env.ENV_VAR }}/file1

Ansible fail with:

fatal: [distant-host] => One or more undefined variables: 'dict object' has no attribute 'ENV_VAR'
7 Answers

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.

If you can run as root, you can use runuser.

- shell: runuser -l '{{ install_user }}' -c "{{ cmd }}"

This effectively runs the command as install_user in a fresh login shell, as if you had used su - *install_user* (which loads the profile, though it might be .bash_profile and not .profile...) and then executed *cmd*.

I'd try not to run everything as root just so you can run it as someone else, though...

If you can modify the configuration of your target host and don't want to change your ansible yaml code. You can try this:
add the variable ENV_VAR=/usr/users/toto into /etc/environment file rather than ~/.profile.

  shell: "bash -l scala -version"

by using bash -l will allow ansible to load corresponding bash_profile.

bash: '-i' (interactive shell) won't allow the ansible to run other task.

Related