Ansible - 使用delegate_to以不同用户的身份在同一台远程计算机上运行任务

Ansible - using delegate_to run tasks on the same remote machine as a different user

提问人:Aditya Siddarth 提问时间:11/15/2023 更新时间:11/15/2023 访问量:33

问:

我有一个剧本,可以在远程节点上执行一些任务。要求是我必须以一个用户(比如 foo-user)的身份执行其中一些任务,而以不同的用户(比如 bar-user)执行其他任务。因此,我在清单主机文件中为同一远程节点 IP 定义了两个不同的条目(node-foo 和 node-bar),然后在 playbook 级别定义了 hosts:node-foo。对于我想以 bar-user 身份执行的那些任务,我使用了 delegate_to: node-bar 选项。

在 playbook 执行期间,它在使用 delegate_to 的第一个任务中失败。我知道它无法访问第一个用户(foo-user)作为远程节点上的第二个用户(bar-user)创建的远程tmp目录,因此任务执行失败。

有没有办法在不给远程 tmp 目录 777 个权限的情况下解决这个问题?

Ansible-2.x

评论


答:

1赞 Vladimir Botka 11/15/2023 #1

使用 remote_user 而不是 delegate_to。例如

- hosts: test_01

  tasks:

    - command: whoami
      register: out
      remote_user: alice
    - debug:
        var: out.stdout

    - command: whoami
      register: out
      remote_user: bob
    - debug:
        var: out.stdout

给出(删节)

  out.stdout: alice
  out.stdout: bob

默认情况下,remote_user创建remote_tmp目录 ~/.ansible/tmp

shell> ssh alice@test_01 ls -la /home/alice/.ansible/ | grep tmp
drwx------  6 alice  alice   6 Nov 15 06:34 tmp
shell> ssh bob@test_01 ls -la /home/bob/.ansible/ | grep tmp
drwx------  6 bob  bob   6 Nov 15 06:34 tmp

Q: “需要为用户指定 ansible become 密码。”

答:如果您出于任何原因决定不使用无密码升级

shell> sudo cat /usr/local/etc/sudoers
  ...
alice ALL=(ALL) ALL
bob ALL=(ALL) ALL
  ...

并且还有更多的远程用户应该升级权限,将密码放入 vars 中。还有更多选项:

  • 例如,在 group_vars_all 中使用文件(并对其进行加密)
shell> cat group_vars/all/ansible_become_pass.yml 
abp:
  alice: alice
  bob: bob

在任务中使用密码。例如

    - command: whoami
      become: true
      register: out
      remote_user: alice
      vars:
        ansible_become_pass: "{{ abp.alice }}"
    - debug:
        var: out.stdout

    - command: whoami
      become: true
      register: out
      remote_user: bob
      vars:
        ansible_become_pass: "{{ abp.bob }}"
    - debug:
        var: out.stdout

TASK [command] **************
changed: [test_01]

TASK [debug] ****************
ok: [test_01] => 
  out.stdout: root

TASK [command] **************
changed: [test_01]

TASK [debug] ****************
ok: [test_01] => 
  out.stdout: root

  • (可选)将密码放入单个文件中(并对其进行加密)
shell> cat vault/abp_alice.yml
alice
shell> cat vault/abp_bob.yml
bob

然后,以下任务

    - command: whoami
      become: true
      register: out
      remote_user: alice
      vars:
        ansible_become_pass: "{{ lookup('file', 'vault/abp_alice.yml') }}"
    - debug:
        var: out.stdout

    - command: whoami
      become: true
      register: out
      remote_user: bob
      vars:
        ansible_become_pass: "{{ lookup('file', 'vault/abp_bob.yml') }}"
    - debug:
        var: out.stdout

按预期工作并给予

TASK [command] *******************************************************************************
changed: [test_01]

TASK [debug] *********************************************************************************
ok: [test_01] => 
  out.stdout: root

TASK [command] *******************************************************************************
changed: [test_01]

TASK [debug] *********************************************************************************
ok: [test_01] => 
  out.stdout: root
  • 如果远程主机之间的密码不同,请使用字典
shell> cat group_vars/all/ansible_become_pass.yml 
abp:
  test_01:
    alice: alice_01
    bob: bob_01
  test_02:
    alice: alice_02
    bob: bob_02

并在任务中使用它

    - command: whoami
      become: true
      register: out
      remote_user: alice
      vars:
        ansible_become_pass: "{{ abp[inventory_hostname]['alice'] }}"

注意:您可以使用关键字remote_user和变量ansible_user来配置“用于登录远程服务器的用户名”。要指定启用模式的密码,只有两个选项:

  • 提供 --ask-become-pass 命令行选项
  • 设置 ansible_become_password 连接变量

看看 become 插件的参数become_pass

        become_pass:
            description: password
            ini:
              - section: runas_become_plugin
                key: password
            vars:
              - name: ansible_become_password
              - name: ansible_become_pass
              - name: ansible_runas_pass
            env:
              - name: ANSIBLE_BECOME_PASS
              - name: ANSIBLE_RUNAS_PASS

您可以看到同一变量的三个别名:

  • ansible_become_password
  • ansible_become_pass
  • ansible_runas_pass

评论

0赞 Aditya Siddarth 11/24/2023
在这种情况下,您能否让我知道定义主机文件的更好方法?请注意,还需要为用户指定 ansible become 密码才能执行一些任务
0赞 Vladimir Botka 11/24/2023
如何定义主机由您决定。将密码放入 vars 中。我加了一个例子。为什么更多的用户升级到root很重要?为什么只有一个还不够?
0赞 Aditya Siddarth 11/27/2023
请帮助我了解在这种情况下ansible_user与remote_user有何不同。我已经在我的清单主机文件中指定了ansible_user,并且按照您的建议,ansible 成为密码。但是,它对我不起作用。我也点击了您分享的链接来理解这一点。但是,我没有完全理解它。
1赞 Vladimir Botka 11/27/2023
请参阅 ssh 连接的参数remote_userremote_useransible_user 都提供“用于登录远程服务器的用户名”。唯一的区别是 remote_user 是一个参数,也可以用作关键字配置,但 ansible_user 是可变的。
0赞 Vladimir Botka 11/27/2023
您已在清单主机文件中指定了ansible_user,但这对您不起作用。您很可能会覆盖变量。请参阅了解变量优先级。显示你得到的:- debug: var=ansible_user