通过item和with_items  对重复操作进行循环执行
示例:
- hosts: jack6_1
 remote_user: root
 gather_facts: no
 tasks:
- name: touch file
 file:
 path: "{{item}}"
 state: touch
 with_items:
- "a"
- "b"
- "c"
在jack6_1主机上创建三个文件,由于是重复执行file模块,可以循环执行示例:
 
 
- hosts: jack6_1
 remote_user: root
 vars:
 dirs:
- "a"
- "b"
- "c"
 files:
- "1"
- "2"
- "3"
 tasks:
- name: remove dir
 file:
 path: "{{item}}"
 state: absent
 with_items: "{{dirs}}"
- name: touch file
 file:
 path: "{{item}}"
 state: touch
 with_items: "{{files}}"
- name: remove files
 file:
 path: "{{item}}"
 state: absent
 with_items: "{{files}}"
 
 
如下定义多个变量,重复执行某个模块
ansible进阶循环
原文:https://blog.51cto.com/13434656/2529200