linux中playbook的控制语句
- 使用 when 判断语句
- block-rescue判断
- 循环语句
1 tasks:
2 ‐ name: aa
3 模块1
4 when: 条件1 [root@pp ~]# mkdir demo3
[root@pp ~]# cp ansible.cfg hosts demo3/
[root@pp ~]# cd demo3/
[root@pp demo3]#
[root@pp demo3]# cat when-1.yaml
---
- hosts: uptasks:- name: tasks1debug: msg="111"when: 1<2
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-1.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [tasks1] ****************************************************************************
ok: [up] => {"msg": "111"
}PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@pp demo3]# cat when-1.yaml
---
- hosts: uptasks:- name: tasks1debug: msg="111"when: 1<2 or 2>3
[root@pp demo3]# ansible-playbook when-1.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [tasks1] ****************************************************************************
ok: [up] => {"msg": "111"
}PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@pp demo3]# cat when-2.yaml
---
- hosts: uptasks:- name: tasks2debug: msg="111"when: ansible_distribution_major_version == "7"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-2.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [tasks2] ****************************************************************************
skipping: [up]PLAY RECAP *******************************************************************************
up : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 [root@pp demo3]# cat when-2.yaml
---
- hosts: uptasks:- name: tasks2debug: msg="111"when: ansible_distribution_major_version == "8"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-2.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [tasks2] ****************************************************************************
ok: [up] => {"msg": "111"
}PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@pp demo3]#
1 value in 列表 [root@pp demo3]# cat when-3.yaml
---
- hosts: upvars:list1: [1,2,3,4]tasks:- name: task3debug: msg="333"when: 2 in list1
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-3.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [task3] *****************************************************************************
ok: [up] => {"msg": "333"
}PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@pp demo3]# cat when-3.yaml
---
- hosts: upvars:list1: [1,2,3,4]tasks:- name: task3debug: msg="333"when: 2 not in list1
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-3.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [task3] *****************************************************************************
skipping: [up]PLAY RECAP *******************************************************************************
up : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0 [root@pp demo3]#
[root@pp demo3]# cat /root/demo2/9-inventory1.yaml
---
- hosts: dbtasks:- name: 打印我在清单文件中的名称debug: msg={{inventory_hostname}}when: inventory_hostname in groups ['xx']
[root@pp demo3]#
[root@pp demo3]# cat when-4.yaml
---
- hosts: upvars:aa: 1bb:tasks:- name: tasks1debug: msg="111"when: aa is undefined- name: tasks2debug: msg="222"when: bb is undefined- name: tasks3debug: msg="333"when: cc is not defined
[root@pp demo3]#
[root@pp demo3]# ansible-playbook when-4.yamlPLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [tasks1] ****************************************************************************
skipping: [up]TASK [tasks2] ****************************************************************************
skipping: [up]TASK [tasks3] ****************************************************************************
ok: [up] => {"msg": "333"
}PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0 1 block:
2 ‐ 模块1
3 ‐ 模块2
4 ‐ 模块3
5 rescue:
6 ‐ 模块1
7 ‐ 模块2

[root@pp demo3]# cat block-1.yaml
---
- hosts: uptasks:- name: task1block:- name: 11debug: msg="1111"- name: 22shell: "ls /aa.txt"- name: 33debug: msg="3333"rescue:- name: xxdebug: msg="xxxx"- name: yydebug: msg="yyyy"- name: task2debug: msg="zzzz"[root@pp demo3]#
[root@pp demo3]# ansible-playbook block-1.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [debug] *****************************************************************************
ok: [up] => {"msg": "1111"
}TASK [shell] *****************************************************************************
fatal: [up]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.009915", "end": "2023-12-22 11:38:31.526934", "msg": "non-zero return code", "rc": 2, "start": "2023-12-22 11:38:31.517019", "stderr": "ls: 无法访问'/aa.txt': 没有那个文件或目录", "stderr_lines": ["ls: 无法访问'/aa.txt': 没有那个文件或目录"], "stdout": "", "stdout_lines": []}TASK [xx] ********************************************************************************
ok: [up] => {"msg": "xxxx"
}TASK [yy] ********************************************************************************
ok: [up] => {"msg": "yyyy"
}TASK [task2] *****************************************************************************
ok: [up] => {"msg": "zzzz"
}PLAY RECAP *******************************************************************************
up : ok=5 changed=0 unreachable=0 failed=0 skipped=0 rescued=1 ignored=0 32.3 循环语句
1 for i in A B C ... ; do
2 命令 $
3 done 1 employee:
2 ‐ uname: lisi
3 age: 22
4 sex: man
5
6 ‐ uname: wangwu
7 age: 24
8 sex: man
9
10 ‐ uname: xiaohua
11 age: 21
[root@pp demo3]# cat loop-1.yaml
---
- hosts: upvars:users:- uname: tomage: 20sex: man- uname: bobage: 22sex: man- uname: maryage: 20sex: womantasks:- name: task1debug: msg={{ item }}loop: "{{ users }}"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook loop-1.yaml
PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [task1] *****************************************************************************
ok: [up] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {"msg": {"age": 20,"sex": "man","uname": "tom"}
}
ok: [up] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {"msg": {"age": 22,"sex": "man","uname": "bob"}
}
ok: [up] => (item={'uname': 'mary', 'age': 20, 'sex': 'woman'}) => {"msg": {"age": 20,"sex": "woman","uname": "mary"}
}PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@pp demo3]# cat loop-1.yaml
---
- hosts: upvars:users:- uname: tomage: 20sex: man- uname: bobage: 22sex: man- uname: maryage: 20sex: womantasks:- name: task1debug: msg={{ item.uname }}loop: "{{ users }}"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook loop-1.yaml PLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [task1] *****************************************************************************
ok: [up] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {"msg": "tom"
}
ok: [up] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {"msg": "bob"
}
ok: [up] => (item={'uname': 'mary', 'age': 20, 'sex': 'woman'}) => {"msg": "mary"
}PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@pp demo3]# cat loop-1.yaml
---
- hosts: upvars:users:- uname: tomage: 20sex: man- uname: bobage: 22sex: man- uname: maryage: 20sex: womantasks:- name: task1debug: msg={{ item.uname }}when: item.sex == "man"loop: "{{ users }}"
[root@pp demo3]#
[root@pp demo3]# ansible-playbook loop-1.yamlPLAY [up] ********************************************************************************TASK [Gathering Facts] *******************************************************************
ok: [up]TASK [task1] *****************************************************************************
ok: [up] => (item={'uname': 'tom', 'age': 20, 'sex': 'man'}) => {"msg": "tom"
}
ok: [up] => (item={'uname': 'bob', 'age': 22, 'sex': 'man'}) => {"msg": "bob"
}
skipping: [up] => (item={'uname': 'mary', 'age': 20, 'sex': 'woman'}) PLAY RECAP *******************************************************************************
up : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 [root@pp demo3]#
相关文章:
linux中playbook的控制语句
本章主要介绍 playbook中的控制语句。 使用 when 判断语句 block-rescue判断 循环语句 一个play中可以包含多个task,如果不想所有的task全部执行,可以设置只有满足某个 条件才执行这个task,不满足条件则不执行此task。本章主要讲解when 和 …...
MongoDB介绍
一、MongoDB介绍 1.1 mongoDB介绍 MongoDB 是由C语言编写的,是一个基于分布式文件存储的开源数据库系统。 在高负载的情况下,添加更多的节点,可以保证服务器性能。 MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。 MongoDB …...
再看参数校验
作者简介:大家好,我是smart哥,前中兴通讯、美团架构师,现某互联网公司CTO 联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬 写一个接口,…...
计算机存储术语: 扇区,磁盘块,页
扇区(sector) 硬盘的读写以扇区为基本单位。磁盘上的每个磁道被等分为若干个弧段,这些弧段称之为扇区。硬盘的物理读写以扇区为基本单位。通常情况下每个扇区的大小是 512 字节。linux 下可以使用 fdisk -l 了解扇区大小: $ sudo /sbin/fdisk -l Disk …...
解决IDEA编译/启动报错:Abnormal build process termination
报错信息 报错信息如下: Abnormal build process termination: "D:\Software\Java\jdk\bin\java" -Xmx3048m -Djava.awt.headlesstrue -Djava.endorsed.dirs\"\" -Djdt.compiler.useSingleThreadtrue -Dpreload.project.path………………很纳…...
Jetpack DataStore
文章目录 Jetpack DataStore概述DataStore 对比 SP添加依赖库Preferences DataStore路径创建 Preferences DataStore获取数据保存数据修改数据删除数据清除全部数据 Proto DataStore配置AndroidStudio安装插件配置proto文件创建序列化器 创建 Proto DataStore获取数据保存数据修…...
在Portainer创建Nginx容器并部署Web静态站点实现公网访问
🔥博客主页: 小羊失眠啦. 🎥系列专栏:《C语言》 《数据结构》 《Linux》《Cpolar》 ❤️感谢大家点赞👍收藏⭐评论✍️ 前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,…...
泛微e-cology XmlRpcServlet文件读取漏洞复现
漏洞介绍 泛微新一代移动办公平台e-cology不仅组织提供了一体化的协同工作平台,将组织事务逐渐实现全程电子化,改变传统纸质文件、实体签章的方式。泛微OA E-Cology 平台XmRpcServlet接口处存在任意文件读取漏洞,攻击者可通过该漏洞读取系统重要文件 (如数据库配置…...
当下流行的直播技术demo演示
nginx-http-flv-module(更新不是很频繁) SRS: https://ossrs.net/lts/zh-cn/(独立官网,目前最新稳定版version5) 基于SRS搭建直播demo演示: 一、搭建流媒体服务器 参见官网:https://ossrs.ne…...
Zabbix自动发现并注册已安装agent的主机
先在被监控主机上安装好zabbix-agent 然后登录zabbix网页 点击发现动作后会出现第三步 然后编辑操作,发现后加入到主机组群 然后编辑发现规则 然后就可以在主机列表中看到被发现的主机。...
Jtti:linux搭建开源ldap服务器的方法
搭建开源LDAP服务器是一种用于集中管理用户身份认证和授权信息的方法。在Linux系统上,OpenLDAP是一个流行的开源LDAP实现,可以用于搭建LDAP服务器。以下是搭建OpenLDAP服务器的基本步骤: 步骤一:安装OpenLDAP 安装OpenLDAP软件包&…...
Gazebo GUI模型编辑器
模型编辑器 现在我们将构建我们的简单机器人。我们将制作一个轮式车辆,并添加一个传感器,使我们能够让机器人跟随一个斑点(人)。 模型编辑器允许我们直接在图形用户界面 (GUI) 中构建简单的模型。对于更复…...
pycharm运行正常,但命令行执行提示module不存在的多种解决方式
问题描述 在执行某个测试模块时出现提示,显示自定义模块data不存在,但是在PyCharm下运行正常。错误信息如下: Traceback (most recent call last):File "/run/channelnterface-autocase/testcases/test_chanel_detail.py", line 2…...
GBASE南大通用GBase 8a ODBC的安装文件
GBASE南大通用GBase 8a ODBC 体系结构是基于五个组件,在下图中所示: GBase 8a ODBC 体系结构图 应用 应用是通过调用 ODBC API 实现对 GBase 数据访问的程序。应用使用标准的 ODBC 调用与驱动程序管理器通信。应用并不关心数据存储在哪里ÿ…...
重新配置torch1.8 cuda11.1 torchtext0.9.0虚拟Pytorch开发环境
这里写目录标题 起因发现选择安装cuda 11.1核对下自己的显卡是否支持下载该版本的CUDACUDA下载地址CUDA安装过程 在anaconda中创建一个虚拟环境1.以下是环境的配置过程2.查看虚拟环境列表3.激活虚拟环境 安装torch和torchtext包的过程1.输入下面这句代码,就可以直接…...
【动画图解】一次理清九大排序算法!面试官问到再也不慌!
排序算法 交换排序 冒泡排序快速排序 插入排序 直接插入排序希尔排序 选择排序 简单选择排序堆排序 归并排序基数排序桶排序 一、冒泡排序 冒泡排序是一种简单的交换排序算法,以升序排序为例,其核心思想是: 从第一个元素开始,…...
组播地址段及其作用
作用 组播(Multicast)传输:在发送者和每一接收者之间实现点对多点网络连接。如果一台发送者同时给多个的接收者传输相同的数据,也只需复制一份的相同数据包。它提高了数据传送效率。减少了骨干网络出现拥塞的可能性。 地址段 组播协议的地址在 IP 协议中属于 D 类…...
Vue+ElementUI前端添加展开收起搜索框按钮
1、搜索框添加判断 v-if"advanced" <el-form-item label"创建日期" v-if"advanced"><el-date-pickerv-model"daterangeLedat"size"small"style"width: 240px"value-format"yyyy-MM-dd"type&q…...
速盾网络:sdk游戏盾有什么作用?
速盾cdn是一款非常优秀的CDN加速服务,它能够帮助游戏开发者们提升游戏的性能和稳定性。其中,速盾cdn的sdk游戏盾是其一项非常实用的功能,它能够为游戏提供更加稳定和快速的网络连接。 首先,让我们来了解一下什么是sdk游戏…...
理解BeEF的架构
BeEF的组件和工作原理BeEF(The Browser Exploitation Framework)是一款用于浏览器渗透测试和漏洞利用的强大工具。它由多个组件组成,这些组件协同工作以实现对受害者浏览器的控制和攻击。本文将深入探讨BeEF的各个组件和其工作原理࿰…...
WaveTools鸣潮工具箱:3大核心功能解锁60帧流畅游戏体验
WaveTools鸣潮工具箱:3大核心功能解锁60帧流畅游戏体验 【免费下载链接】WaveTools 🧰鸣潮工具箱 项目地址: https://gitcode.com/gh_mirrors/wa/WaveTools 为什么《鸣潮》玩家需要一款专业工具箱?当你在开放世界中探索时,…...
1.NCM格式解密技术全解析:从原理到实战的音乐自由之路
1.NCM格式解密技术全解析:从原理到实战的音乐自由之路 【免费下载链接】ncmdump ncmdump - 网易云音乐NCM转换 项目地址: https://gitcode.com/gh_mirrors/ncmdu/ncmdump 问题引入:当音乐遭遇数字围栏 "花了千元订阅的无损音乐,…...
手把手教你用PLECS画波德图:从AC Sweep设置到看懂相位裕度,避坑指南
从零开始掌握PLECS波德图分析:工程师必备的频域诊断手册 第一次在PLECS里点击"AC Sweep"按钮时,我盯着满屏的参数选项发呆了十分钟。作为电力电子工程师,我们总说"看波德图就像看电路的体检报告",但当你真正面…...
告别手动上传!RAGFlow 0.22.0 数据源同步实战:以S3和Notion为例的保姆级配置
告别手动上传!RAGFlow 0.22.0 数据源同步实战:以S3和Notion为例的保姆级配置 如果你还在为知识库维护中频繁的手动上传文件而烦恼,RAGFlow 0.22.0版本的数据源功能将成为你的效率救星。这个功能彻底改变了传统文件管理方式,让数据…...
USB2.0供电那些事儿:为什么你的外设总是供电不足?
USB2.0供电困境解析:从原理到实践的全面解决方案 当你的移动硬盘突然断开连接,或者外接键盘间歇性失灵时,很可能正遭遇USB2.0供电不足的经典难题。这种看似简单的接口背后,隐藏着复杂的电力分配机制与设备兼容性博弈。本文将带你穿…...
手把手教你用Dockerfile为Ubuntu 18.04镜像定制Python+OpenCV开发环境
从零构建PythonOpenCV的Docker开发环境:最佳实践指南 在计算机视觉和机器学习项目中,一个标准化、可复现的开发环境至关重要。Docker作为容器化技术的代表,能够完美解决"在我机器上能跑"的经典难题。本文将手把手教你如何基于Ubunt…...
从《巴伦周刊》谈起,我们该如何保住 SRE 的直觉?
大多数 AI 依然停留在执行层面,它们只能在 Demo 里写写脚本。一旦丢进真实的生产集群,面对复杂的资源依赖和权限限制,它们很难像人类专家那样,给出真正能拍板的建议。最近,《巴伦周刊》对 Chaterm 的报道引起了我的注意…...
百度快速排名优化技术(百度seo排名优化)
百度快速排名优化技术是一种针对搜索引擎结果页面(SERP)排名优化的技术手段,通过优化网站的内容、结构和用户体验等方面,提高网站在搜索引擎中的排名,从而获得更多的流量和潜在客户。下面,我将介绍百度快速…...
揭秘联发科设备Bootloader解锁:mtkclient-gui实战指南与深度解析
揭秘联发科设备Bootloader解锁:mtkclient-gui实战指南与深度解析 【免费下载链接】mtkclient-gui GUI tool for unlocking bootloader and bypassing authorization on Mediatek devices (Not maintained anymore) 项目地址: https://gitcode.com/gh_mirrors/mt/m…...
SystemVerilog进阶:深入探索随机化约束的高级应用
1. 从基础到进阶:SystemVerilog随机化约束的核心价值 在芯片验证领域,随机化验证已经成为提高验证效率的黄金标准。SystemVerilog的随机化约束机制,就像给验证工程师配备了一个智能数据生成器,可以自动产生符合设计规范的测试场景…...
