Ansible Loop循环控制

在Ansible 2.5之前主要使用with_ <lookup>关键字来创建循环,循环关键字基本上类似于with_list,with_items。

Ansible 2.5开始就可以使用loop来创建循环。
 

示例1 数组循环:

  tasks:

    - name: create user

      user:

          name: {{ item }}

          loop:

          - user01

          - user02

          - user03

示例2 字典循环:

tasks:

- name: create user and group

  user:

        name: {{ item.name }}

        group: {{ item.groups }}

        loop:

<span><span><span><span><span><span><span><span><span><span>        - name<span><span>:<span><span> <span><span>‘testuser1‘</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span>          groups<span><span>:<span><span> <span><span>‘wheel‘<span><span><span><span><br /></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<span><span><span><span>        -<span><span> <span><span><span><span>name<span><span>:<span><span> <span><span>‘testuser2‘</span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

<span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span><span>          groups<span><span>:<span><span> <span><span>‘root‘<span><span><span><span><br /></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>

未完待续...

相关推荐