1.with_list
最简单的列表循环。
---
- name: just test
hosts: liqitest2
tasks:
- name: just echo
debug:
msg: "{{ item }}"
with_list:
- 1
- [2,4,[3,6]]
- 5
(ansible5.6) [root@ansible-server ansible]# ansible-playbook liqitest.yml
PLAY [just test] *************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************
ok: [192.168.226.101]
TASK [just echo] *************************************************************************************************************************
ok: [192.168.226.101] => (item=1) => {
"msg": 1
}
ok: [192.168.226.101] => (item=[2, 4, [3, 6]]) => {
"msg": [
2,
4,
[
3,
6
]
]
}
ok: [192.168.226.101] => (item=5) => {
"msg": 5
}
PLAY RECAP *******************************************************************************************************************************
192.168.226.101 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2.with_items
with_items和with_list比起来,with_items会将循环中的列表展开,但是之展开一级。
---
- name: just test
hosts: liqitest2
tasks:
- name: just echo
debug:
msg: "{{ item }}"
with_items:
- 1
- [2,4,[3,6]]
- 5
(ansible5.6) [root@ansible-server ansible]# ansible-playbook liqitest.yml
PLAY [just test] *************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************
ok: [192.168.226.101]
TASK [just echo] *************************************************************************************************************************
ok: [192.168.226.101] => (item=1) => {
"msg": 1
}
ok: [192.168.226.101] => (item=2) => {
"msg": 2
}
ok: [192.168.226.101] => (item=4) => {
"msg": 4
}
ok: [192.168.226.101] => (item=[3, 6]) => {
"msg": [
3,
6
]
}
ok: [192.168.226.101] => (item=5) => {
"msg": 5
}
PLAY RECAP *******************************************************************************************************************************
192.168.226.101 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3.with_flatten
with_flatten和with_items比起来,with_flatten会将所有层级的列表展开,不想items只展开一级。
---
- name: just test
hosts: liqitest2
tasks:
- name: just echo
debug:
msg: "{{ item }}"
with_flattened:
- 1
- [2,4,[3,6]]
- 5
(ansible5.6) [root@ansible-server ansible]# ansible-playbook liqitest.yml
PLAY [just test] *************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************
ok: [192.168.226.101]
TASK [just echo] *************************************************************************************************************************
ok: [192.168.226.101] => (item=1) => {
"msg": 1
}
ok: [192.168.226.101] => (item=2) => {
"msg": 2
}
ok: [192.168.226.101] => (item=4) => {
"msg": 4
}
ok: [192.168.226.101] => (item=3) => {
"msg": 3
}
ok: [192.168.226.101] => (item=6) => {
"msg": 6
}
ok: [192.168.226.101] => (item=5) => {
"msg": 5
}
PLAY RECAP *******************************************************************************************************************************
192.168.226.101 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
4.with_file
循环获取ansible控制节点(而非托管节点)文件的内容。
---
- name: just test
hosts: liqitest2
tasks:
- name: just echo
debug:
msg: "{{ item }}"
with_file:
- /root/test1.file
- /root/test2.file
(ansible5.6) [root@ansible-server ansible]# ansible-playbook liqitest.yml
PLAY [just test] *************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************
ok: [192.168.226.101]
TASK [just echo] *************************************************************************************************************************
ok: [192.168.226.101] => (item=test file 1) => {
"msg": "test file 1"
}
ok: [192.168.226.101] => (item=test file 2) => {
"msg": "test file 2"
}
PLAY RECAP *******************************************************************************************************************************
192.168.226.101 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
5.with_fileglob
循环获取ansible控制节点(而非托管节点)匹配到的文件名。支持通配符。
---
- name: just test
hosts: liqitest2
tasks:
- name: just echo
debug:
msg: "{{ item }}"
with_fileglob:
- /root/*.file
(ansible5.6) [root@ansible-server ansible]# ansible-playbook liqitest.yml
PLAY [just test] *************************************************************************************************************************
TASK [Gathering Facts] *******************************************************************************************************************
ok: [192.168.226.101]
TASK [just echo] *************************************************************************************************************************
ok: [192.168.226.101] => (item=/root/test1.file) => {
"msg": "/root/test1.file"
}
ok: [192.168.226.101] => (item=/root/test2.file) => {
"msg": "/root/test2.file"
}
PLAY RECAP *******************************************************************************************************************************
192.168.226.101 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
。。。持续且漫长更新。。。