目录标题
1. 在playbook中使用循环with_items
[root@server4 ~]# vim /etc/ansible/hosts
172.25.60.2
testB.westos.com ansible_host=172.25.60.1
testC ansible_host=172.25.60.6
[testA]
test1 ansible_host=172.25.60.5
[testB]
172.25.60.3
[test:children]
testA
testB
with_items:将groups.ungrouped中返回的信息,一条一条的放到item中,并一条一条输出
[root@server4 ~]# cat xh.yml
---
- hosts: testB
remote_user: root
gather_facts: no # 禁止testB这台主机的fact的信息
tasks:
- debug:
msg: "{
{item}}"
with_items: "{
{groups.ungrouped}}"
测试:
[root@server4 ~]# ansible-playbook xh.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => (item=172.25.60.2) => {
"msg": "172.25.60.2"
}
ok: [172.25.60.3] => (item=testB.westos.com) => {
"msg": "testB.westos.com"
}
ok: [172.25.60.3] => (item=testC) => {
"msg": "testC"
}
PLAY RECAP *********************************************************************
172.25.60.3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2. 自定义with_items的值输出
[root@server4 ~]# cat xh.yml
---
- hosts: testB
remote_user: root
gather_facts: no # 禁止手机testB这台主机的fact的信息
tasks:
- debug:
msg: "{
{item}}"
with_items: # 或者[1,2,3]
- 1
- 2
- 3
测试:
[root@server4 ~]# ansible-playbook xh.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => (item=1) => {
"msg": 1
}
ok: [172.25.60.3] => (item=2) => {
"msg": 2
}
ok: [172.25.60.3] => (item=3)