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的各个组件和其工作原理࿰…...

大型活动交通拥堵治理的视觉算法应用
大型活动下智慧交通的视觉分析应用 一、背景与挑战 大型活动(如演唱会、马拉松赛事、高考中考等)期间,城市交通面临瞬时人流车流激增、传统摄像头模糊、交通拥堵识别滞后等问题。以演唱会为例,暖城商圈曾因观众集中离场导致周边…...

从深圳崛起的“机器之眼”:赴港乐动机器人的万亿赛道赶考路
进入2025年以来,尽管围绕人形机器人、具身智能等机器人赛道的质疑声不断,但全球市场热度依然高涨,入局者持续增加。 以国内市场为例,天眼查专业版数据显示,截至5月底,我国现存在业、存续状态的机器人相关企…...
大语言模型如何处理长文本?常用文本分割技术详解
为什么需要文本分割? 引言:为什么需要文本分割?一、基础文本分割方法1. 按段落分割(Paragraph Splitting)2. 按句子分割(Sentence Splitting)二、高级文本分割策略3. 重叠分割(Sliding Window)4. 递归分割(Recursive Splitting)三、生产级工具推荐5. 使用LangChain的…...
【HTML-16】深入理解HTML中的块元素与行内元素
HTML元素根据其显示特性可以分为两大类:块元素(Block-level Elements)和行内元素(Inline Elements)。理解这两者的区别对于构建良好的网页布局至关重要。本文将全面解析这两种元素的特性、区别以及实际应用场景。 1. 块元素(Block-level Elements) 1.1 基本特性 …...
LLM基础1_语言模型如何处理文本
基于GitHub项目:https://github.com/datawhalechina/llms-from-scratch-cn 工具介绍 tiktoken:OpenAI开发的专业"分词器" torch:Facebook开发的强力计算引擎,相当于超级计算器 理解词嵌入:给词语画"…...

全志A40i android7.1 调试信息打印串口由uart0改为uart3
一,概述 1. 目的 将调试信息打印串口由uart0改为uart3。 2. 版本信息 Uboot版本:2014.07; Kernel版本:Linux-3.10; 二,Uboot 1. sys_config.fex改动 使能uart3(TX:PH00 RX:PH01),并让boo…...
《C++ 模板》
目录 函数模板 类模板 非类型模板参数 模板特化 函数模板特化 类模板的特化 模板,就像一个模具,里面可以将不同类型的材料做成一个形状,其分为函数模板和类模板。 函数模板 函数模板可以简化函数重载的代码。格式:templa…...
JS设计模式(4):观察者模式
JS设计模式(4):观察者模式 一、引入 在开发中,我们经常会遇到这样的场景:一个对象的状态变化需要自动通知其他对象,比如: 电商平台中,商品库存变化时需要通知所有订阅该商品的用户;新闻网站中࿰…...
为什么要创建 Vue 实例
核心原因:Vue 需要一个「控制中心」来驱动整个应用 你可以把 Vue 实例想象成你应用的**「大脑」或「引擎」。它负责协调模板、数据、逻辑和行为,将它们变成一个活的、可交互的应用**。没有这个实例,你的代码只是一堆静态的 HTML、JavaScript 变量和函数,无法「活」起来。 …...
C语言中提供的第三方库之哈希表实现
一. 简介 前面一篇文章简单学习了C语言中第三方库(uthash库)提供对哈希表的操作,文章如下: C语言中提供的第三方库uthash常用接口-CSDN博客 本文简单学习一下第三方库 uthash库对哈希表的操作。 二. uthash库哈希表操作示例 u…...