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的各个组件和其工作原理࿰…...
【大模型RAG】拍照搜题技术架构速览:三层管道、两级检索、兜底大模型
摘要 拍照搜题系统采用“三层管道(多模态 OCR → 语义检索 → 答案渲染)、两级检索(倒排 BM25 向量 HNSW)并以大语言模型兜底”的整体框架: 多模态 OCR 层 将题目图片经过超分、去噪、倾斜校正后,分别用…...
Vue记事本应用实现教程
文章目录 1. 项目介绍2. 开发环境准备3. 设计应用界面4. 创建Vue实例和数据模型5. 实现记事本功能5.1 添加新记事项5.2 删除记事项5.3 清空所有记事 6. 添加样式7. 功能扩展:显示创建时间8. 功能扩展:记事项搜索9. 完整代码10. Vue知识点解析10.1 数据绑…...
论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(二)
HoST框架核心实现方法详解 - 论文深度解读(第二部分) 《Learning Humanoid Standing-up Control across Diverse Postures》 系列文章: 论文深度解读 + 算法与代码分析(二) 作者机构: 上海AI Lab, 上海交通大学, 香港大学, 浙江大学, 香港中文大学 论文主题: 人形机器人…...
3.3.1_1 检错编码(奇偶校验码)
从这节课开始,我们会探讨数据链路层的差错控制功能,差错控制功能的主要目标是要发现并且解决一个帧内部的位错误,我们需要使用特殊的编码技术去发现帧内部的位错误,当我们发现位错误之后,通常来说有两种解决方案。第一…...
ssc377d修改flash分区大小
1、flash的分区默认分配16M、 / # df -h Filesystem Size Used Available Use% Mounted on /dev/root 1.9M 1.9M 0 100% / /dev/mtdblock4 3.0M...
YSYX学习记录(八)
C语言,练习0: 先创建一个文件夹,我用的是物理机: 安装build-essential 练习1: 我注释掉了 #include <stdio.h> 出现下面错误 在你的文本编辑器中打开ex1文件,随机修改或删除一部分,之后…...
ElasticSearch搜索引擎之倒排索引及其底层算法
文章目录 一、搜索引擎1、什么是搜索引擎?2、搜索引擎的分类3、常用的搜索引擎4、搜索引擎的特点二、倒排索引1、简介2、为什么倒排索引不用B+树1.创建时间长,文件大。2.其次,树深,IO次数可怕。3.索引可能会失效。4.精准度差。三. 倒排索引四、算法1、Term Index的算法2、 …...
【C语言练习】080. 使用C语言实现简单的数据库操作
080. 使用C语言实现简单的数据库操作 080. 使用C语言实现简单的数据库操作使用原生APIODBC接口第三方库ORM框架文件模拟1. 安装SQLite2. 示例代码:使用SQLite创建数据库、表和插入数据3. 编译和运行4. 示例运行输出:5. 注意事项6. 总结080. 使用C语言实现简单的数据库操作 在…...
涂鸦T5AI手搓语音、emoji、otto机器人从入门到实战
“🤖手搓TuyaAI语音指令 😍秒变表情包大师,让萌系Otto机器人🔥玩出智能新花样!开整!” 🤖 Otto机器人 → 直接点明主体 手搓TuyaAI语音 → 强调 自主编程/自定义 语音控制(TuyaAI…...
项目部署到Linux上时遇到的错误(Redis,MySQL,无法正确连接,地址占用问题)
Redis无法正确连接 在运行jar包时出现了这样的错误 查询得知问题核心在于Redis连接失败,具体原因是客户端发送了密码认证请求,但Redis服务器未设置密码 1.为Redis设置密码(匹配客户端配置) 步骤: 1).修…...
