配置好清单

vim /etc/ansible/hosts
172.25.254.2
aplox.bbcc.com ansible_host=172.25.254.3
server6

[testA]
test2 ansible_host=172.25.254.5

[testB]
172.25.254.4

[test:children]
testA
testB

ansible循环控制 ansible for循环_嵌套


问题是不确定返回信息时如何处理

with_items

外部接收信息循环
---
- hosts: testB
  remote_user: root
  gather_facts: no                          禁止ansible收集facts信息(主机信息)
  tasks:
  - debug:
      msg: "{{item}}"                       将返回变量收集然后分放到每个item中
    with_items: "{{groups.ungrouped}}"      用with_items接受模块信息

ansible循环控制 ansible for循环_嵌套_02

内部接收信息循环
1.
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_items: 
    - 1
    - 2
    - 3

ansible循环控制 ansible for循环_排列组合_03

2.列表循环
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_items: [1,2,3]

ansible循环控制 ansible for循环_ide_04

3.对象循环

拿到对象中定义的值

---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item.test1}}"
    with_items:
    - {test1: a,test2: b}
    - {test1: c,test2: d}

ansible循环控制 ansible for循环_嵌套_05

同一个主机内创建4个文件

---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - name: touch file
    file: path=/testdir/{{item}} state=touch
    with_items: [file1,file2,file3,file4]

ansible循环控制 ansible for循环_嵌套_06

---
- hosts: testB
  remote_user: root
  vars:
    dirs:
    - "/testdir/a"
    - "/testdir/b"
    - "/testdir/c"
    - "/testdir/d"

  tasks:
  - file:
      path: "{{item}}"
      state: touch
    with_items: "{{dirs}}"

ansible循环控制 ansible for循环_嵌套_07

shell循环

提取注册变量

---
- hosts: testB
  remote_user: root
  tasks:
  - shell: "ls /opt"
    register: returnvalue
  - debug:
      var: returnvalue

shell命令循环

---
- hosts: testB
  remote_user: root
  tasks:
  - shell: "{{item}}"
    with_items:
    - "ls /testdir"
    - "ls /root"
    register: returnvalue
  - debug:
      var: returnvalue

== 取其中某一部分==

---
- hosts: testB
  remote_user: root
  tasks:
  - shell: "{{item}}"
    with_items:
    - "ls /testdir"
    - "ls /root"
    register: returnvalue
  - debug:
      #var: returnvalue
      #msg: "{{returnvalue.results}}"
      msg: "{{item.stdout_lines}}"
    with_items: "{{returnvalue.results}}"

列表嵌套循环及各关键字

[[1,2,3],[4,5,6]]
对各元素循环
---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "{{item}}"
    with_items:
    - [1,2,3]
    - [4,5,6]

ansible循环控制 ansible for循环_排列组合_08

对子列表循环
---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "{{item}}"
    with_list:
    - [1,2,3]
    - [4,5,6]

ansible循环控制 ansible for循环_嵌套_09

with_flattened效果通with_items一样 拉平
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_flattened:
    - [1,2,3]
    - [4,5,6]

ansible循环控制 ansible for循环_嵌套_10

with_together根据索引合并输出
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_together:
    - [1,2,3]
    - [4,5,6]

ansible循环控制 ansible for循环_ide_11


缺的用null补齐

---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_together:
    - [1,2,3]
    - [4,5]

ansible循环控制 ansible for循环_嵌套_12

用通配创建目录或文件

ansible循环控制 ansible for循环_排列组合_13

with_cartesian表示将各列表排列组合
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_cartesian:
    - [a,b,c]
    - [file1,file2]

ansible循环控制 ansible for循环_排列组合_14

排列组合生成文件或目录
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - file:
      path: "/testdir/{{item.0}}/{{item.1}}"
      state: directory
    with_cartesian:
    - [a,b,c]
    - [file1,file2]

ansible循环控制 ansible for循环_ansible循环控制_15

with_nested通with_cartesian效果一样
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - file:
      path: "/testdir/{{item.0}}/{{item.1}}"
      state: directory
    with_nested:
    - [a,b,c]
    - [file1,file2]
with_indexed_items为列表增加数字索引
---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "index is : {{item.0}} , value is {{item.1}}"
    with_indexed_items:
    - a
    - b
    - c

ansible循环控制 ansible for循环_排列组合_16

嵌套一层会拉平

---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "index is : {{item.0}} , value is {{item.1}}"
    with_indexed_items:
    - [a,b,c]
    - [d,e]
    - [f,g,h]

ansible循环控制 ansible for循环_嵌套_17


嵌套第二层就不会拉平

---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "index is : {{item.0}} , value is {{item.1}}"
    with_indexed_items:
    - [a,b,c]
    - [d,[e,o]]
    - [f,g,h]

ansible循环控制 ansible for循环_ansible循环控制_18

注意:with_flattened嵌套会进行拉平 with_items不会拉平

with_sequence 效果同python range
指定距离 步长


---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "{{item}}"
    with_sequence: start=1 end=5 stride=1


---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "{{item}}"
    with_sequence: 
      start=1 
      end=5 
      stride=1


---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "{{item}}"
    with_sequence: count=5

ansible循环控制 ansible for循环_嵌套_19

如果循环从大到小,步长为-

---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "{{item}}"
    with_sequence: start=6 end=2 stride=-2

ansible循环控制 ansible for循环_排列组合_20

用循环创建redhat{2,4,6,8,10}文件
---
- hosts: testB
  remote_user: root
  tasks:
  - file:
      path: "/testdir/redhat{{item}}"
      state: touch
    with_sequence: start=2 end=10 stride=2

ansible循环控制 ansible for循环_ansible循环控制_21

ansible循环控制 ansible for循环_ansible循环控制_22

with_random_choice 随机返回值
---
- hosts: testB
  remote_user: root
  tasks:
  - debug:
      msg: "{{item}}"
    with_random_choice:
    - 1
    - 2
    - 3
    - 4
with_dict 字典形式循环
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    users:
      lily: female
      alex: male
  tasks:
  - debug:
      msg: "{{item}}"
    with_dict: "{{users}}"

ansible循环控制 ansible for循环_排列组合_23

---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    users:
      lily: female
      alex: male
  tasks:
  - debug:
      msg: "name: {{item.key}} gender: {{item.value}}"
    with_dict: "{{users}}"

ansible循环控制 ansible for循环_ide_24

嵌套字典循环输出
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    users:
      lily: 
        name: lilly
        gender: female
        telephones: 156-546-894
      alex: 
        name: aliex
        gender: male
        telephones: 213-346-358
  tasks:
  - debug:
      msg: "{{item}}"
    with_dict: "{{users}}"

ansible循环控制 ansible for循环_ide_25

---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    users:
      lily:
        name: lilly
        gender: female
        telephones: 156-546-894
      alex:
        name: aliex
        gender: male
        telephones: 213-346-358
  tasks:
  - debug:
      msg: "User {{item.key}} is {{item.value.name}}, gender: {{item.value.gender}}, tel: {{item.value.telephones}}"
    with_dict: "{{users}}"

ansible循环控制 ansible for循环_嵌套_26

with_subelements:对列表中1对多进行循环
---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    users:
    - name: lily
      gender: female
      hobby:
        - Videogame
        - Basketball
    - name: alex
      gender: female
      hobby:
        - football
  tasks:
  - debug:
      msg: "{{item}}"
    with_subelements:
    - "{{users}}"
    - hobby

ansible循环控制 ansible for循环_排列组合_27

---
- hosts: testB
  remote_user: root
  gather_facts: no
  vars:
    users:
    - name: lily
      gender: female
      hobby:
        - Videogame
        - Basketball
    - name: alex
      gender: female
      hobby:
        - football
  tasks:
  - debug:
      msg: "{{item.0.name}} 's hobby is {{item.1}}"
    with_subelements:
    - "{{users}}"
    - hobby

ansible循环控制 ansible for循环_排列组合_28

with_file:无论目标主机是谁 都可以获取到ansible主机的文件
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_file:
    - /testdir/aplox
    - /opt/rico

ansible循环控制 ansible for循环_ansible循环控制_29

with_fileglob: 匹配一个或多个目录下的所有文件
---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_fileglob:
    - /testdir/*

ansible循环控制 ansible for循环_ansible循环控制_30

---
- hosts: testB
  remote_user: root
  gather_facts: no
  tasks:
  - debug:
      msg: "{{item}}"
    with_fileglob:
    - /testdir/*
    - /opt/test*.???

ansible循环控制 ansible for循环_排列组合_31