ansible:with_indexed_items、with_sequence、 with_random_choice


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
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值