目录标题
1. with_indexed_items给输出添加数字索引
[root@server4 ~]# cat xh6.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "index :{{item.0}}, value: {{item.1}}"
with_indexed_items:
- test1
- test2
- test3
测试:
[root@server4 ~]# ansible-playbook xh6.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => (item=[0, u'test1']) => {
"msg": "index :0, value: test1"
}
ok: [172.25.60.3] => (item=[1, u'test2']) => {
"msg": "index :1, value: test2"
}
ok: [172.25.60.3] => (item=[2, u'test3']) => {
"msg": "index :2, value: test3"
}
PLAY RECAP *********************************************************************
172.25.60.3 : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
2. with_indexed_items中使用列表嵌套列表
[root@server4 ~]# cat xh6.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "index :{{item.0}}, value: {{item.1}}"
with_indexed_items:
- [test1,testa]
- [test2,testb]
- [test3,[testc,testd]]
测试:列表中嵌套的子列表不会被拆开
[root@server4 ~]# ansible-playbook xh6.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => (item=[0, u'test1']) => {
"msg": "index :0, value: test1"
}
ok: [172.25.60.3] => (item=[1, u'testa']) => {
"msg": "index :1, value: testa"
}
ok: [172.25.60.3] => (item=[2, u'test2']) => {
"msg": "index :2, value: test2"
}
ok: [172.25.60.3] => (item=[3, u'testb']) => {
"msg": "index :3, value: testb"
}
ok: [172.25.60.3] => (item=[4, u'test3']) => {
"msg": "index :4, value: test3"
}
ok: [172.25.60.3] => (item=[5, [u'testc', u'testd']]) => {
"msg": "index :5, value: [u'testc', u'testd']"
}
3. with_sequence指定序列的开始和结尾以及步长
with_sequence:start=1 end=5 stride=1 # 表示从1开始到5结束,并且间隔为1
[root@server4 ~]# cat xh7.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_sequence:
start=1
end=5
stride=1
测试:
[root@server4 ~]# ansible-playbook xh7.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) => {
"msg": "3"
}
ok: [172.25.60.3] => (item=4) => {
"msg": "4"
}
ok: [172.25.60.3] => (item=5) => {
"msg": "5"
}
count=5表示从1开始到5结束步长为
[root@server4 ~]# cat xh7.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_sequence: count=5
测试:
[root@server4 ~]# ansible-playbook xh7.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) => {
"msg": "3"
}
ok: [172.25.60.3] => (item=4) => {
"msg": "4"
}
ok: [172.25.60.3] => (item=5) => {
"msg": "5"
}
如果start比end大,stride应该为负数
[root@server4 ~]# cat xh7.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{ item }}"
with_sequence: start=6 end=2 stride=-2
测试:
[root@server4 ~]# ansible-playbook xh7.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => (item=6) => {
"msg": "6"
}
ok: [172.25.60.3] => (item=4) => {
"msg": "4"
}
ok: [172.25.60.3] => (item=2) => {
"msg": "2"
}
4. 创建偶数数列的目录westos2,westos4,westos6,westos8,westos10
[root@server4 ~]# cat xh8.yml
---
- hosts: testB
remote_user: root
tasks:
- file:
path: "/testdir/westos{{item}}"
state: directory
with_sequence:
start=2
end=10
stride=2
测试:
[root@server3 testdir]# ls
westos10 westos2 westos4 westos6 westos8
5. with_random_choice:随机选择一个数返回
[root@server4 ~]# cat xh9.yml
---
- hosts: testB
remote_user: root
gather_facts: no
tasks:
- debug:
msg: "{{item}}"
with_random_choice:
- 1
- 2
- 3
- 4
- 5
测试:
[root@server4 ~]# ansible-playbook xh9.yml
PLAY [testB] *******************************************************************
TASK [debug] *******************************************************************
ok: [172.25.60.3] => (item=4) => {
"msg": 4
}