linux--ansible(playbook7 循环with)

配置好清单

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

在这里插入图片描述
问题是不确定返回信息时如何处理

with_items

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

3.对象循环

拿到对象中定义的值

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

在这里插入图片描述

同一个主机内创建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]

在这里插入图片描述

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

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

在这里插入图片描述

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]

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述
缺的用null补齐

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

在这里插入图片描述

用通配创建目录或文件

在这里插入图片描述

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

在这里插入图片描述

排列组合生成文件或目录
---
- 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]

在这里插入图片描述

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

在这里插入图片描述

嵌套一层会拉平

---
- 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]

在这里插入图片描述
嵌套第二层就不会拉平

---
- 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]

在这里插入图片描述

注意: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

在这里插入图片描述

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

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

在这里插入图片描述

用循环创建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

在这里插入图片描述在这里插入图片描述

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}}"

在这里插入图片描述

---
- 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}}"

在这里插入图片描述

嵌套字典循环输出
---
- 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}}"

在这里插入图片描述

---
- 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}}"

在这里插入图片描述

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

在这里插入图片描述

---
- 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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

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

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值