ansible 迭代 with_items

迭代:with_items

迭代:当有需要重复性执行的任务时,可以使用迭代机制

  • 对迭代项的引用,固定变量为 “item”
  • 要在task中使用with_items给顶要迭代的元素列表
  • 列表格式:
    字符串
    字典

例子:

---
- hosts: all
  remote_user: root
  
  tasks:
    - name: create groups
      file: name=/data/{{ item }} state=touch
      when: ansible_distribution_major_version == "7"
      with_items:
        - file1
        - file2
        - file3
    - name: install some packages
      yum: name={{ item }}
      with_items:
        - htop
        - sl
        - hping3

迭代嵌套子变量

- hosts: websrvs
  remote_user: root
  tasks:
    - name: add some groups
      group: name={{item}} state=present
      with_ites:
        - group1
        - group2
        - group3
   - name: add some users
     user: name={{item.name}} group={{item.group}} state=present
     with_items:
        - {name: 'user1', group: 'group1'}
        - {name: 'user2', group: 'group2'}
posted @ 2021-09-06 18:07  Star-Hitian  阅读(170)  评论(0)    收藏  举报