主机列表DEV和QA主机中有两个主机
我需要使用dev_sudo_user为开发环境执行playbook,并为qA环境并行执行qa_sudo_user
库存明细
[hostlist]
host1 ansible_become_user=dev_sudo_user
host2 ansible_become_user=qa_sudo_user
---
- hosts: hostlist
connection: ssh
gather_facts: false
remote_user: abcd
serial: 1
become: true
tasks:
- name: run Script
shell: python apps.py
register: result
- debug: var=result
我能够获得一个主机的结果。我想要两个主机的结果
答案 0 :(得分:0)
您必须使用become
和become_user
参数。请参阅下面的示例和输出。
使用您拥有的变量托管文件:
[test_group]
rhel-green become_user=root
rhel-blue become_user=devops
剧本:
- hosts: test_group
gather_facts: false
tasks:
- name: step 1
shell: "id"
register: shell_output
become_user: "{{ become_user }}"
become: true
- debug:
var: shell_output.stdout
执行输出:
[root@ansible]# ansible-playbook -i hosts become_loop.yml
PLAY [test_group] ***************************************************************************************************************************************************************************************************
TASK [step 1] *******************************************************************************************************************************************************************************************************
changed: [rhel-blue]
changed: [rhel-green]
TASK [debug] ********************************************************************************************************************************************************************************************************
ok: [rhel-green] => {
"shell_output.stdout": "uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023"
}
ok: [rhel-blue] => {
"shell_output.stdout": "uid=1000(devops) gid=1000(devops) groups=1000(devops) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023"
}
PLAY RECAP **********************************************************************************************************************************************************************************************************
rhel-blue : ok=2 changed=1 unreachable=0 failed=0
rhel-green : ok=2 changed=1 unreachable=0 failed=0
[root@ansible]#