Search code examples
ansibleenvironment

ansible become_user does not take environment settings


I have linux box, where there is a user "user1", using C shell. The user has .cshrc in its home directory with some useful environment settings.

when I use this ansible logic though, the environment is not set properly for the user.

---
- name: some playbook
  hosts: remote_host
  become: yes
  become_user: root
  become_method: sudo

  tasks:
    - name: Check environment variables for user1
      become: yes
      become_user: user1
      become_method: sudo 
      shell: "env"
      register: envresult

    - name: debug env
      debug:
        var: envresult.stdout

In the output I can see that the variables set in .cshrc are not in the environment. How can I force ansible to process the login scripts of users upon become?

Thank you!


Solution

  • You should add become_flags: "-i" to your playbook. The task then looks like

        - name: Check environment variables for user1
          become: yes
          become_user: user1
          become_method: sudo 
          become_flags: "-i"
          shell: "env"
          register: envresult
    

    More Information is available at the Ansible documentation.