Ansible playbook之循环

devtools/2024/10/19 7:34:57/

1.标准Loops

        当我们想安装10个软件包的时候,为了避免写10个task来安装,我们可以直接使用标准的loops简单快速实现10个软件包的安装,下面例子是分别打印了one two这两个值:

#1.编写loop.yaml
[root@ansible01 ansible]# cat loops.yaml 
---
- hosts: 11.0.1.18gather_facts: Falsetasks:- name: debug loopsdebug: msg="name --> {{ item }}"with_items:- one- two
#2.运行loop.yaml文件
[root@ansible01 ansible]# ansible-playbook loops.yaml 
PLAY [11.0.1.18] **************************************************************************************************************************************************************************TASK [debug loops] ************************************************************************************************************************************************************************
ok: [11.0.1.18] => (item=one) => {"msg": "name --> one"
}
ok: [11.0.1.18] => (item=two) => {"msg": "name --> two"
}PLAY RECAP ********************************************************************************************************************************************************************************
11.0.1.18                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

        with_items的值是python list数据结构,可以理解为每个task会循环读取list里面的值,然后key的名称是item,当然list里面也支持python字典,如下:

[root@ansible01 ansible]# cat loops2.yaml 
---
- hosts: 11.0.1.18gather_facts: Falsetasks:- name: debug loopsdebug: msg="name --> {{ item.key }} value -->{{ item.value}}"with_items:- {key: "one",value: "hello"}- {key: "two",value: "wyx"}[root@ansible01 ansible]# ansible-playbook loops2.yaml 
PLAY [11.0.1.18] **************************************************************************************************************************************************************************TASK [debug loops] ************************************************************************************************************************************************************************
ok: [11.0.1.18] => (item={u'value': u'hello', u'key': u'one'}) => {"msg": "name --> one value -->hello"
}
ok: [11.0.1.18] => (item={u'value': u'wyx', u'key': u'two'}) => {"msg": "name --> two value -->wyx"
}PLAY RECAP ********************************************************************************************************************************************************************************
11.0.1.18                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

2.嵌套Loops

        嵌套Loops也是我们编写playbook中比较常见的一种循环,它主要实现一对多或者多对多的合并,如下:

[root@ansible01 ansible]# cat loops3.yaml 
---
- hosts: 11.0.1.18gather_facts: Falsetasks:- name: debug loopsdebug: msg="name --> {{ item[0] }} value --> {{ item[1] }}"with_nested:- ['A']- ['a','b','c']
[root@ansible01 ansible]# ansible-playbook loops3.yaml 
PLAY [11.0.1.18] **************************************************************************************************************************************************************************TASK [debug loops] ************************************************************************************************************************************************************************
ok: [11.0.1.18] => (item=[u'A', u'a']) => {"msg": "name --> A value --> a"
}
ok: [11.0.1.18] => (item=[u'A', u'b']) => {"msg": "name --> A value --> b"
}
ok: [11.0.1.18] => (item=[u'A', u'c']) => {"msg": "name --> A value --> c"
}PLAY RECAP ********************************************************************************************************************************************************************************
11.0.1.18                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

3. 散列loops

        散列loops相比标准的loops就是变量支持更丰富的数据结构,比如标准loops的最外层数据必须是Python的list数据类型,而散列loops直接支持YAML格式的数据变量。如下:

[root@ansible01 ansible]# cat loops4.yaml 
---
- hosts: 11.0.1.18gather_facts: Falsevars:user:shencan:name: shencanshell: bashruifengyun:name: ruifengyunshell: zshtasks:- name: debug loopsdebug: msg="name --> {{ item.key }} value --> {{ item.value.name }} shell --> {{ item.value.shell }}"with_dict: "{{ user }}"[root@ansible01 ansible]# ansible-playbook loops4.yaml
PLAY [11.0.1.18] **************************************************************************************************************************************************************************TASK [debug loops] ************************************************************************************************************************************************************************
ok: [11.0.1.18] => (item={u'key': u'ruifengyun', u'value': {u'shell': u'zsh', u'name': u'ruifengyun'}}) => {"msg": "name --> ruifengyun value --> ruifengyun shell --> zsh"
}
ok: [11.0.1.18] => (item={u'key': u'shencan', u'value': {u'shell': u'bash', u'name': u'shencan'}}) => {"msg": "name --> shencan value --> shencan shell --> bash"
}PLAY RECAP ********************************************************************************************************************************************************************************
11.0.1.18                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

4. 文件匹配loops

        我们针对一个目录下指定格式的文件进行处理时,可以直接引用with_fileglob循环去匹配我们需要处理的文件即可,如下:

[root@ansible01 ansible]# cat /etc/ansible/loops5.yaml 
---
- hosts: 11.0.1.18gather_facts: Falsetasks:- name: debug loopsdebug: msg="files --> {{ item }}"with_fileglob:- /etc/ansible/*.yaml 
[root@ansible01 ansible]# ansible-playbook loops5.yaml
PLAY [11.0.1.18] **************************************************************************************************************************************************************************TASK [debug loops] ************************************************************************************************************************************************************************
ok: [11.0.1.18] => (item=/etc/ansible/loops2.yaml) => {"msg": "files --> /etc/ansible/loops2.yaml"
}
ok: [11.0.1.18] => (item=/etc/ansible/loops3.yaml) => {"msg": "files --> /etc/ansible/loops3.yaml"
}
ok: [11.0.1.18] => (item=/etc/ansible/loops5.yaml) => {"msg": "files --> /etc/ansible/loops5.yaml"
}
ok: [11.0.1.18] => (item=/etc/ansible/loops4.yaml) => {"msg": "files --> /etc/ansible/loops4.yaml"
}
ok: [11.0.1.18] => (item=/etc/ansible/variable.yaml) => {"msg": "files --> /etc/ansible/variable.yaml"
}
ok: [11.0.1.18] => (item=/etc/ansible/variable2.yaml) => {"msg": "files --> /etc/ansible/variable2.yaml"
}
ok: [11.0.1.18] => (item=/etc/ansible/loops.yaml) => {"msg": "files --> /etc/ansible/loops.yaml"
}PLAY RECAP ********************************************************************************************************************************************************************************
11.0.1.18                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

with_fileglob 这个时候会匹配/etc/ansible下所有的yaml结尾的文件,当作{{ item }}变量来引用。

原理是使用了python glob模块去做的文件模糊匹配。

5. 随机选择loops

        with_random_choice会从传入的list里面随机选择一个,作为item引用的参数

[root@ansible01 ansible]# cat loops6.yaml 
---
- hosts: 11.0.1.18gather_facts: Falsetasks:- name: debug loopsdebug: msg="names --> {{ item }}"with_random_choice:- "ansible1"- "ansible2"- "ansible3"
[root@ansible01 ansible]# ansible-playbook loops6.yaml 
PLAY [11.0.1.18] **************************************************************************************************************************************************************************TASK [debug loops] ************************************************************************************************************************************************************************
ok: [11.0.1.18] => (item=ansible3) => {"msg": "names --> ansible3"
}PLAY RECAP ********************************************************************************************************************************************************************************
11.0.1.18                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

6. 条件判断loops

        有时候执行一个task后,我们需要检测这个task的结果是否达到了预想状态,如果没有达到我们预想的状态时,就需要退出整个playbook执行,这个时候我们需要对某个task结果一直进行循环检测,如下:

[root@ansible01 ansible]# cat loops7.yaml 
---
- hosts: 11.0.1.18gather_facts: Falsetasks:- name: debug loopsshell: cat /etc/ansible/hostsregister: hostuntil: host.stdout.startswith("Master")retries: 5delay: 5
[root@ansible01 ansible]# ansible-playbook loops7.yaml
PLAY [11.0.1.18] **************************************************************************************************************************************************************************TASK [debug loops] ************************************************************************************************************************************************************************
FAILED - RETRYING: debug loops (5 retries left).
FAILED - RETRYING: debug loops (4 retries left).
FAILED - RETRYING: debug loops (3 retries left).
FAILED - RETRYING: debug loops (2 retries left).
FAILED - RETRYING: debug loops (1 retries left).
fatal: [11.0.1.18]: FAILED! => {"attempts": 5, "changed": true, "cmd": "cat /etc/ansible/hosts", "delta": "0:00:00.005700", "end": "2024-04-30 09:49:27.614588", "rc": 0, "start": "2024-04-30 09:49:27.608888", "stderr": "", "stderr_lines": [], "stdout": "# This is the default ansible 'hosts' file.\n#\n# It should live in /etc/ansible/hosts\n#\n#   - Comments begin with the '#' character\n#   - Blank lines are ignored\n#   - Groups of hosts are delimited by [header] elements\n#   - You can enter hostnames or ip addresses\n#   - A hostname/ip can be a member of multiple groups\n\n# Ex 1: Ungrouped hosts, specify before any group headers.\n\n## green.example.com\n## blue.example.com\n## 192.168.100.1\n## 192.168.100.10\n\n# Ex 2: A collection of hosts belonging to the 'webservers' group\n\n## [webservers]\n## alpha.example.org\n## beta.example.org\n## 192.168.1.100\n## 192.168.1.110\n\n# If you have multiple hosts following a pattern you can specify\n# them like this:\n\n## www[001:006].example.com\n\n# Ex 3: A collection of database servers in the 'dbservers' group\n\n## [dbservers]\n## \n## db01.intranet.mydomain.net\n## db02.intranet.mydomain.net\n## 10.25.1.56\n## 10.25.1.57\n\n# Here's another example of host ranges, this time there are no\n# leading 0s:\n#11.0.1.18\tkey=118\n#11.0.1.19\tkey=119\n[nginx]\n11.0.1.1[8:9]\n[nginx:vars]\nansible_python_interpreter=/usr/bin/python2.7", "stdout_lines": ["# This is the default ansible 'hosts' file.", "#", "# It should live in /etc/ansible/hosts", "#", "#   - Comments begin with the '#' character", "#   - Blank lines are ignored", "#   - Groups of hosts are delimited by [header] elements", "#   - You can enter hostnames or ip addresses", "#   - A hostname/ip can be a member of multiple groups", "", "# Ex 1: Ungrouped hosts, specify before any group headers.", "", "## green.example.com", "## blue.example.com", "## 192.168.100.1", "## 192.168.100.10", "", "# Ex 2: A collection of hosts belonging to the 'webservers' group", "", "## [webservers]", "## alpha.example.org", "## beta.example.org", "## 192.168.1.100", "## 192.168.1.110", "", "# If you have multiple hosts following a pattern you can specify", "# them like this:", "", "## www[001:006].example.com", "", "# Ex 3: A collection of database servers in the 'dbservers' group", "", "## [dbservers]", "## ", "## db01.intranet.mydomain.net", "## db02.intranet.mydomain.net", "## 10.25.1.56", "## 10.25.1.57", "", "# Here's another example of host ranges, this time there are no", "# leading 0s:", "#11.0.1.18\tkey=118", "#11.0.1.19\tkey=119", "[nginx]", "11.0.1.1[8:9]", "[nginx:vars]", "ansible_python_interpreter=/usr/bin/python2.7"]}PLAY RECAP ********************************************************************************************************************************************************************************
11.0.1.18                  : ok=0    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

        5s执行一次cat /etc/ansible/hosts,将结果register给host然后判断host.stdout.startswith的内容是否Master字符串开头,如果条件成立,此task运行完成,如果条件不成立5s后再重试,5次后还不成立,此task运行失败。

7. 文件优先匹配loops

        这个与第4个文件匹配类似,就新增了一个优选项,如下:

[root@ansible01 ansible]# cat loops8.yaml 
---
- hosts: 11.0.1.18gather_facts: Falsetasks:- name: debug loopsdebug: msg="files --> {{ item }}"with_first_found:- "wyx.yaml"
[root@ansible01 ansible]# ansible-playbook loops8.yaml
PLAY [11.0.1.18] **************************************************************************************************************************************************************************TASK [debug loops] ************************************************************************************************************************************************************************
ok: [11.0.1.18] => (item=/etc/ansible/wyx.yaml) => {"msg": "files --> /etc/ansible/wyx.yaml"
}PLAY RECAP ********************************************************************************************************************************************************************************
11.0.1.18                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   


http://www.ppmy.cn/devtools/27225.html

相关文章

【数据结构】链表经典算法OJ题目练习

203. 移除链表元素 - 力扣(LeetCode) 思路1:遍历原链表,将val链表释放掉 思路2:创建新链表 206. 反转链表 - 力扣(LeetCode) 思路1:创建新链表,将原链表中的节点拿过来…

商超物联网方案-Hotspot Service和客流分析方案概述

商超物联网方案-Hotspot Service和客流分析方案概述 场景概述 大型商场、大型综合体在相互竞争及线上消费的影响下,利润增长缓慢,迫切需要通过提供个性化服务提升顾客购物体验,促进利润增长。 向不同顾客推送其感兴趣的广告,不仅…

启明云端2.4寸屏+ESP32-S3+小型智能调速电动家用除草机案例 触控三档调速,能显示电压故障码

今天给大家分享个启明云端2.4寸屏ESP32-S3小型智能调速电动家用除草机案例,国外有草坪文化,这个机器能智能触控三档调速,带屏能显示电压故障码,数显档位(3档最大),触控屏,长按3秒就能…

三种滤波(EKF、UKF、CKF)的对比,含MATLAB源代码

使用MATLAB模拟三维的滤波,包含扩展卡尔曼滤波EKF、无迹卡尔曼滤波UKF、容积卡尔曼滤波CKF。 状态更新和观测更新均为非线性的,模拟一定强度的机动性,可用于卡尔曼滤波方法的对比学习,自己修改成需要的运动模型后,可以用于组合导航(GPS+DVL形式)。 运行结果 真值的三轴…

深入了解Java中的Thread类

在Java编程中,Thread类是一个核心的类,用于创建和管理线程。线程是程序执行的最小单元,多线程编程可以提高程序的并发性和效率。 本文将深入介绍Java中的Thread类,包括其基本概念、创建线程的方法、线程状态的转换、线程同步与通…

iOS 11 iPhone X 适配总结

前言 随着iPhone 8、iPhone X和iOS 11的到来,我们可以制作出比以往体验更加优秀的APP,适配也随之而来。下面我们介绍下一些新特性! 参考文章 这里总结了大家iOS 11,iPhone X 适配问题.如有问题,欢迎大家讨论. iOS …

BTCOIN发布WEB3.0论坛:生态与金融的双重叙事热点驱动自由创新意识

在数字时代,信息的自由流动和透明度是推动经济发展和社会进步的关键。尤其在加密货币和区块链领域,这一点尤为重要。BTCOIN的最新创举——一个基于WEB3.0理念的信息论坛,不仅标志着信息传递自由化的新篇章,也为数字货币市场的信息…

Go语言nil概念,make与new的区别

nil 在Go语言中,nil 是一种特殊值,主要用于指针、接口、切片、映射、通道这五种引用类型。与其它类型的默认值(零值)有着显著的区别: nil: nil 表示没有具体的值或不存在的对象引用。它可以赋值给指针、切…