ansible become_user 不带环境设置

ansible become_user does not take environment settings

我有 linux 框,其中有一个用户“user1”,使用 C shell。用户在其主目录中有 .cshrc 以及一些有用的环境设置。

当我使用这个 ansible 逻辑时,没有为用户正确设置环境。

---
- 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

在输出中我可以看到.cshrc 中设置的变量不在环境中。如何强制 ansible 处理用户的登录脚本?

谢谢!

您应该将 become_flags: "-i" 添加到您的剧本中。然后任务看起来像

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

更多信息可在 Ansible documentation