Ansible playbook之循环

2024-05-01 07:36
文章标签 循环 ansible playbook

本文主要是介绍Ansible playbook之循环,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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   

这篇关于Ansible playbook之循环的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/951084

相关文章

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

Nginx部署React项目时重定向循环问题的解决方案

《Nginx部署React项目时重定向循环问题的解决方案》Nginx在处理React项目请求时出现重定向循环,通常是由于`try_files`配置错误或`root`路径配置不当导致的,本文给大家详细介... 目录问题原因1. try_files 配置错误2. root 路径错误解决方法1. 检查 try_f

Spring三级缓存解决循环依赖的解析过程

《Spring三级缓存解决循环依赖的解析过程》:本文主要介绍Spring三级缓存解决循环依赖的解析过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、循环依赖场景二、三级缓存定义三、解决流程(以ServiceA和ServiceB为例)四、关键机制详解五、设计约

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

C# foreach 循环中获取索引的实现方式

《C#foreach循环中获取索引的实现方式》:本文主要介绍C#foreach循环中获取索引的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录一、手动维护索引变量二、LINQ Select + 元组解构三、扩展方法封装索引四、使用 for 循环替代

Spring Boot循环依赖原理、解决方案与最佳实践(全解析)

《SpringBoot循环依赖原理、解决方案与最佳实践(全解析)》循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,:本文主要介绍SpringBoot循环依赖原理、解决方案与最... 目录一、循环依赖的本质与危害1.1 什么是循环依赖?1.2 核心危害二、Spring的三级缓存机制2.1 三

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Python循环缓冲区的应用详解

《Python循环缓冲区的应用详解》循环缓冲区是一个线性缓冲区,逻辑上被视为一个循环的结构,本文主要为大家介绍了Python中循环缓冲区的相关应用,有兴趣的小伙伴可以了解一下... 目录什么是循环缓冲区循环缓冲区的结构python中的循环缓冲区实现运行循环缓冲区循环缓冲区的优势应用案例Python中的实现库

Java嵌套for循环优化方案分享

《Java嵌套for循环优化方案分享》介绍了Java中嵌套for循环的优化方法,包括减少循环次数、合并循环、使用更高效的数据结构、并行处理、预处理和缓存、算法优化、尽量减少对象创建以及本地变量优化,通... 目录Java 嵌套 for 循环优化方案1. 减少循环次数2. 合并循环3. 使用更高效的数据结构4

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(