第 8.2 章 Playbook流程控制--循环

第 8 章 Playbook流程控制--循环

循环使用with_list或者使用loop(推荐使用)关键字进行

下列示例中,共使用了四种不同的方法,如下:

# cat loop.yml

---

- hosts: web

  gather_facts: yes

  remote_user: root

#使用with_list关键字循环

  tasks:

  - name: with_list

    debug: msg="{{item}}"

    with_list:

      - one

      - two

    tags: add1

#使用loop替换with_list关键字循环

  - name: with_list -> loop

    debug: msg="{{item}}"

    loop:

      - 1

      - 2

    tags: add2

#使用loop添加的用户名进行循环

  - name: add user -> loop

    #debug: msg="{{item}}"

    user: name={{item}} state=present

    loop:

      - longtao1

      - longtao2

    tags: adduser1

#使用loop添加的用户名和用户组的字典模式添加用户进行循环

  - name: add user with-item

    #debug: msg="{{item}}"

    user: name={{item.name}} groups={{item.groups}} state=present

    with_items:

      - {name: 'lt', groups: 'nginx'}

      - {name: 'baiziyou', groups: 'nginx'}

    tags: adduser2

版权声明:
作者:WaterBear
链接:https://l-t.top/2026.html
来源:雷霆运维
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
< <上一篇
下一篇>>
文章目录
关闭
目 录