提问人:Aditya Siddarth 提问时间:11/15/2023 更新时间:11/15/2023 访问量:33
Ansible - 使用delegate_to以不同用户的身份在同一台远程计算机上运行任务
Ansible - using delegate_to run tasks on the same remote machine as a different user
问:
我有一个剧本,可以在远程节点上执行一些任务。要求是我必须以一个用户(比如 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 个权限的情况下解决这个问题?
答:
使用 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
评论
- debug: var=ansible_user
评论