ansible playbook中的循环

2018-08-06

ansible playbook 中的循环

  例:

[root@server ansible]# vim loop.yml

---

- hosts: testhosts

  user: root

  tasks:

   - name: change mod for file

     file: path=/tmp/{{item}} mode=600 owner=root group=root

     with_items:

      - 1.txt

      - 2.txt

  针对 file 模块来说,item 是一个变量,那么根据设定 item 中的内容可以把执行过程看成一个循环。

  执行结果:

[root@server ansible]# ansible-playbook loop.yml



PLAY [testhosts] ***************************************************************



TASK [setup] *******************************************************************

ok: [client.test.com]

ok: [127.0.0.1]



TASK [change mod for file] *****************************************************

changed: [client.test.com] => (item=1.txt)

changed: [127.0.0.1] => (item=1.txt)

changed: [client.test.com] => (item=2.txt)

changed: [127.0.0.1] => (item=2.txt)



PLAY RECAP *********************************************************************

127.0.0.1                  : ok=2    changed=1    unreachable=0    failed=0

client.test.com            : ok=2    changed=1    unreachable=0    failed=0

  查看效果:

[root@server ansible]# ansible testhosts -m shell -a "ls -ld /tmp/{1.txt,2.txt}"

client.test.com | SUCCESS | rc=0 >>

-rw------- 1 root root 0 7月  11 01:28 /tmp/1.txt

-rw------- 1 root root 0 7月  11 01:28 /tmp/2.txt



127.0.0.1 | SUCCESS | rc=0 >>

-rw------- 1 root root 0 7月  11 01:28 /tmp/1.txt

-rw------- 1 root root 0 7月  11 01:28 /tmp/2.txt

标题:ansible playbook中的循环
作者:散宜生
地址:https://17kblog.com/articles/2018/08/06/1533519794832.html