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的各个组件和其工作原理࿰…...
Vue记事本应用实现教程
文章目录 1. 项目介绍2. 开发环境准备3. 设计应用界面4. 创建Vue实例和数据模型5. 实现记事本功能5.1 添加新记事项5.2 删除记事项5.3 清空所有记事 6. 添加样式7. 功能扩展:显示创建时间8. 功能扩展:记事项搜索9. 完整代码10. Vue知识点解析10.1 数据绑…...
label-studio的使用教程(导入本地路径)
文章目录 1. 准备环境2. 脚本启动2.1 Windows2.2 Linux 3. 安装label-studio机器学习后端3.1 pip安装(推荐)3.2 GitHub仓库安装 4. 后端配置4.1 yolo环境4.2 引入后端模型4.3 修改脚本4.4 启动后端 5. 标注工程5.1 创建工程5.2 配置图片路径5.3 配置工程类型标签5.4 配置模型5.…...
【Linux】C语言执行shell指令
在C语言中执行Shell指令 在C语言中,有几种方法可以执行Shell指令: 1. 使用system()函数 这是最简单的方法,包含在stdlib.h头文件中: #include <stdlib.h>int main() {system("ls -l"); // 执行ls -l命令retu…...
【JVM】- 内存结构
引言 JVM:Java Virtual Machine 定义:Java虚拟机,Java二进制字节码的运行环境好处: 一次编写,到处运行自动内存管理,垃圾回收的功能数组下标越界检查(会抛异常,不会覆盖到其他代码…...
YSYX学习记录(八)
C语言,练习0: 先创建一个文件夹,我用的是物理机: 安装build-essential 练习1: 我注释掉了 #include <stdio.h> 出现下面错误 在你的文本编辑器中打开ex1文件,随机修改或删除一部分,之后…...
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility
Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...
基础测试工具使用经验
背景 vtune,perf, nsight system等基础测试工具,都是用过的,但是没有记录,都逐渐忘了。所以写这篇博客总结记录一下,只要以后发现新的用法,就记得来编辑补充一下 perf 比较基础的用法: 先改这…...
Mac软件卸载指南,简单易懂!
刚和Adobe分手,它却总在Library里给你写"回忆录"?卸载的Final Cut Pro像电子幽灵般阴魂不散?总是会有残留文件,别慌!这份Mac软件卸载指南,将用最硬核的方式教你"数字分手术"࿰…...
企业如何增强终端安全?
在数字化转型加速的今天,企业的业务运行越来越依赖于终端设备。从员工的笔记本电脑、智能手机,到工厂里的物联网设备、智能传感器,这些终端构成了企业与外部世界连接的 “神经末梢”。然而,随着远程办公的常态化和设备接入的爆炸式…...
排序算法总结(C++)
目录 一、稳定性二、排序算法选择、冒泡、插入排序归并排序随机快速排序堆排序基数排序计数排序 三、总结 一、稳定性 排序算法的稳定性是指:同样大小的样本 **(同样大小的数据)**在排序之后不会改变原始的相对次序。 稳定性对基础类型对象…...
