ansible进阶(二)-循环-常用的with_*有哪些?

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

。。。持续且漫长更新。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值