RHCE8 资料整理(十一)

news2024/9/23 1:30:26

RHCE8 资料整理

    • 第 32 章 控制语句
      • 32.1 判断语句 when
        • 32.1.1 when 判断中>、<、!=和==的使用
        • 32.1.2 when 判断中 in的用法
        • 32.1.3 when 判断中 is的用法
      • 32.2 判断语句 block-rescue
      • 32.3 循环语句

第 32 章 控制语句

一个play中可以包含多个task,如果不想所有的task全部执行,可以设置只有满足某个条件才执行这个task。本章主要介绍whenblock-rescue两种判断语句。

32.1 判断语句 when

when作为一个判断语句,出现在某个task下,格式,

tasks:
- name: xxx
  模块
  when: 条件

如果条件成立,则执行模块,否则不执行。

在when中引用变量时是不用加{{}}的

32.1.1 when 判断中>、<、!=和==的使用

when后面可以有多个判断条件,使用orand作为连接符

常见的判断符包括:

符号说明
==等于
!=不等于
>大于
=>大于等于
<小于
<=小于等于
[root@node-137 ansible]# cat when-1.yml
---
- hosts: db1
  tasks:
  - name: when test
    debug: msg="1<2"
    when: 1<2
  - name: when test2
    debug: msg="1>2"
    when: 1>2
  - debug: msg="1>2 or 2<3"
    when: 1>2 or 2<3
  - debug: msg="2>1 and 3>2"
    when: 2>1 and 3>2
  - debug: msg="centos:7"
    when: ansible_distribution_major_version=="7"
[root@node-137 ansible]# ansible-playbook when-1.yml

PLAY [db1] ****************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-138]

TASK [when test] **********************************************************************************************************************
ok: [node-138] => {
    "msg": "1<2"
}

TASK [when test2] *********************************************************************************************************************
skipping: [node-138]

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "1>2 or 2<3"
}

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "2>1 and 3>2"
}

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "centos:7"
}

PLAY RECAP ****************************************************************************************************************************
node-138                   : ok=5    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0

当判断条件不成立,则会提示skipping

32.1.2 when 判断中 in的用法

在when语句中,除可以使用上面的大于、小于等判断方法外,还可以使用in,如下

value [not] in 列表

如果此值在这个列表中,则判断成立,否则不成立

[root@node-137 ansible]# cat when-3.yml
---
- hosts: db1
  gather_facts: false
  vars:
    list1: [1,2,3,4,5]
  tasks:
  - debug: msg="123"
    when: 1 in list1
  - debug: msg="not in"
    when: 6 not in list1
[root@node-137 ansible]# ansible-playbook when-3.yml

PLAY [db1] ****************************************************************************************************************************

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "123"
}

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "not in"
}

PLAY RECAP ****************************************************************************************************************************
node-138                   : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
32.1.3 when 判断中 is的用法

is可以用于判断变量是否被定义,常见的判断包括以下3种:

  1. is defined,变量被定义
  2. is undefined,变量未被定义,等同于is not defined
  3. is noe,变量被定义了,但值为空
[root@node-137 ansible]# cat when-4.yml
---
- hosts: db1
  gather_facts: true
  vars:
    aa: 123
    dd:
  tasks:
  - debug: msg={{aa}}
    when: aa is defined
  - debug: msg="bb"
    when: bb is not defined
  - debug: msg="cc"
    when: cc is undefined
  - debug: msg="dd"
    when: dd is none
[root@node-137 ansible]# ansible-playbook when-4.yml

PLAY [db1] ****************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-138]

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": 123
}

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "bb"
}

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "cc"
}

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "dd"
}

PLAY RECAP ****************************************************************************************************************************
node-138                   : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

判断文件的小练习

[root@node-137 ansible]# cat when-5.yml
---
- hosts: db1
  tasks:
  - name: run syscmd
    shell: "ls /aa.txt"
    register: aa
    ignore_errors: true
  - name: task2
    fail: msg="命令执行错误"
    when: aa.rc != 0
  - name: task3
    debug: msg="OK123"
[root@node-137 ansible]# ansible-playbook when-5.yml

PLAY [db1] ****************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-138]

TASK [run syscmd] *********************************************************************************************************************
fatal: [node-138]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.037543", "end": "2023-12-20 21:02:05.426760", "msg": "non-zero return code", "rc": 2, "start": "2023-12-20 21:02:05.389217", "stderr": "ls: cannot access /aa.txt: No such file or directory", "stderr_lines": ["ls: cannot access /aa.txt: No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [task2] **************************************************************************************************************************
fatal: [node-138]: FAILED! => {"changed": false, "msg": "命令执行错误"}

PLAY RECAP ****************************************************************************************************************************
node-138                   : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=1

32.2 判断语句 block-rescue

对于when来说,只能做一个判断,成立就执行,不成立就不执行。blockrescue一般同用,类似于shell判断语句中的if-else,在block下面可以包含多个模块,来判断这多个模块是否执行成功。用法,

block:
- 模块1
- 模块2
- 模块3
rescue:
- 模块4
- 模块5

执行流程:

  • 首先执行task1block中的模块1,如果成功,则继续执行block中的模块2,如果block中所有模块均执行成功,则跳过rescue中的模块,进而执行task2中的模块
  • 当执行task1block中的某个模块失败,则执行rescue中的模块4,如果rescue中的所有模块均执行成功,则继续执行task2中的模块,如果rescue中的某个模块失败,则退出playbook
  • 如果blockrescue的模块中,有ignore_errors: yes选项,则会忽略该报错模块

在这里插入图片描述
示例1,参考图1

[root@node-137 ansible]# cat block-1.yml
---
- hosts: db1
  tasks:
  - name: task1
    block:
    - debug: msg="block1"
    - shell: "ls /"
    - debug: msg="block2"
    rescue:
    - debug: msg="rescue1"
    - debug: msg="rescue2"
  - name: task2
    debug: msg="task2"
    
[root@node-137 ansible]# ansible-playbook block-1.yml

PLAY [db1] ****************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-138]

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "block1"
}

TASK [shell] **************************************************************************************************************************
changed: [node-138]

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "block2"
}

TASK [task2] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "task2"
}

PLAY RECAP ****************************************************************************************************************************
node-138                   : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

示例2,参考图2

[root@node-137 ansible]# cat block-1.yml
---
- hosts: db1
  tasks:
  - name: task1
    block:
    - debug: msg="block1"
    - shell: "ls /aa.txt"
    - debug: msg="block2"
    rescue:
    - debug: msg="rescue1"
    - debug: msg="rescue2"
  - name: task2
    debug: msg="task2"
[root@node-137 ansible]# ansible-playbook block-1.yml

PLAY [db1] ****************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-138]

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "block1"
}

TASK [shell] **************************************************************************************************************************
fatal: [node-138]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.032879", "end": "2023-12-20 21:52:57.557622", "msg": "non-zero return code", "rc": 2, "start": "2023-12-20 21:52:57.524743", "stderr": "ls: cannot access /aa.txt: No such file or directory", "stderr_lines": ["ls: cannot access /aa.txt: No such file or directory"], "stdout": "", "stdout_lines": []}

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "rescue1"
}

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "rescue2"
}

TASK [task2] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "task2"
}

PLAY RECAP ****************************************************************************************************************************
node-138                   : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0

示例3,参考图3

[root@node-137 ansible]# cat block-1.yml
---
- hosts: db1
  tasks:
  - name: task1
    block:
    - debug: msg="block1"
    - shell: "ls /aa.txt"
      ignore_errors: true
    - debug: msg="block2"
    rescue:
    - debug: msg="rescue1"
    - debug: msg="rescue2"
  - name: task2
    debug: msg="task2"
[root@node-137 ansible]# ansible-playbook block-1.yml

PLAY [db1] ****************************************************************************************************************************

TASK [Gathering Facts] ****************************************************************************************************************
ok: [node-138]

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "block1"
}

TASK [shell] **************************************************************************************************************************
fatal: [node-138]: FAILED! => {"changed": true, "cmd": "ls /aa.txt", "delta": "0:00:00.035454", "end": "2023-12-20 21:56:08.510199", "msg": "non-zero return code", "rc": 2, "start": "2023-12-20 21:56:08.474745", "stderr": "ls: cannot access /aa.txt: No such file or directory", "stderr_lines": ["ls: cannot access /aa.txt: No such file or directory"], "stdout": "", "stdout_lines": []}
...ignoring

TASK [debug] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "block2"
}

TASK [task2] **************************************************************************************************************************
ok: [node-138] => {
    "msg": "task2"
}

PLAY RECAP ****************************************************************************************************************************
node-138                   : ok=5    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1

32.3 循环语句

在playbook中用loop来循环列表中的元素,在loop中,使用一个固定的变量item,然后把每个元素赋值给item。格式,

list1: [a,b,c]
...
debug: msg={{item}}
loop: "{{list1}}"
#或 
#with_items: "{{list1}}"
  • 这里关键字loop可以替换为with_items,含义相同
  • loopwith_items后面的{{}}需要使用""双引号或''单引号引起来
[root@node-137 ansible]# cat loop-1.yml
---
- hosts: db1
  gather_facts: false
  vars:
    users:
    - uname: "tom"
      age: 18
    - uname: "jerry"
      age: 19
    - uname: "jacky"
      age: 18
  tasks:
  - debug: msg={{ item }}
    with_items: "{{users}}"
  - debug: msg={{item.uname}}
    loop: '{{users}}'
  - debug: msg={{item}}
    when: item.age == 18
    loop: "{{users}}"
[root@node-137 ansible]# ansible-playbook loop-1.yml

PLAY [db1] ****************************************************************************************************************************

TASK [debug] **************************************************************************************************************************
ok: [node-138] => (item={u'uname': u'tom', u'age': 18}) => {
    "msg": {
        "age": 18,
        "uname": "tom"
    }
}
ok: [node-138] => (item={u'uname': u'jerry', u'age': 19}) => {
    "msg": {
        "age": 19,
        "uname": "jerry"
    }
}
ok: [node-138] => (item={u'uname': u'jacky', u'age': 18}) => {
    "msg": {
        "age": 18,
        "uname": "jacky"
    }
}

TASK [debug] **************************************************************************************************************************
ok: [node-138] => (item={u'uname': u'tom', u'age': 18}) => {
    "msg": "tom"
}
ok: [node-138] => (item={u'uname': u'jerry', u'age': 19}) => {
    "msg": "jerry"
}
ok: [node-138] => (item={u'uname': u'jacky', u'age': 18}) => {
    "msg": "jacky"
}

TASK [debug] **************************************************************************************************************************
ok: [node-138] => (item={u'uname': u'tom', u'age': 18}) => {
    "msg": {
        "age": 18,
        "uname": "tom"
    }
}
skipping: [node-138] => (item={u'uname': u'jerry', u'age': 19})
ok: [node-138] => (item={u'uname': u'jacky', u'age': 18}) => {
    "msg": {
        "age": 18,
        "uname": "jacky"
    }
}

PLAY RECAP ****************************************************************************************************************************
node-138                   : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

在最后一个task中,加入判断语句when,使age等于18的user进行打印

with_items还能够迭代字典,而loop不行

[root@node-137 ansible]# cat loop-1.yml
---
- hosts: db1
  gather_facts: false
  vars:
    userdict:
      users:
        uname: "tom"
        age: 18
        age1: 18
      users2:
        uname: "jea"
        age: 19
        age1: 20
  tasks:
  - debug: msg={{ item }}
    ignore_errors: true
    with_items: "{{userdict.users}}"
  - debug: msg={{ item }}
    loop: "{{userdict}}"
[root@node-137 ansible]# ansible-playbook loop-1.yml

PLAY [db1] ****************************************************************************************************************************

TASK [debug] **************************************************************************************************************************
ok: [node-138] => (item=uname) => {
    "msg": "uname"
}
ok: [node-138] => (item=age) => {
    "msg": "age"
}
ok: [node-138] => (item=age1) => {
    "msg": "age1"
}

TASK [debug] **************************************************************************************************************************
fatal: [node-138]: FAILED! => {"msg": "Invalid data passed to 'loop', it requires a list, got this instead: {u'users2': {u'uname': u'jea', u'age': 19, u'age1': 20}, u'users': {u'uname': u'tom', u'age': 18, u'age1': 18}}. Hint: If you passed a list/dict of just one element, try adding wantlist=True to your lookup invocation or use q/query instead of lookup."}

PLAY RECAP ****************************************************************************************************************************
node-138                   : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1325134.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

计算机网络-进阶

目录 易混淆物理层数据链路层网络层nat如何实现私有ip通信IP数据报 格式解析tcp 连接tcp流量控制滑动窗口拥塞控制 报文捕获 wireshark路由模拟器 enspcdn代理服务器 VS cdn VS web cache 计算机有了物理地址&#xff0c;为什么还要有ip地址&#xff1f;单播 多播 广播 传输层会…

EXCEL VLOOKUP函数

参考资料 Excel&#xff1a;史上最全的VLOOKUP应用教程VLOOKUP函数最全面最详细的讲解大全&#xff0c;涵盖17个重要和常见用法&#xff01; 目录 零. 前提条件一. 单条件查找1.1 顺向查找1.2 逆向查找 二. 多条件查找2.1 顺向查找2.2 逆向查找 三. 根据条件查询等级四. 交差查…

excel导出,post还是get请求?

1&#xff0c;前提 今天在解决excel导出的bug时&#xff0c;因为导出接口查询参数较多&#xff0c;所以把原来的get请求接口修改为post请求 原代码&#xff1a; 修改后&#xff1a; 2&#xff0c;修改后 postman请求正常&#xff0c;然后让前端对接口进行同步修改&#xff0…

【图神经网络】在节点分类任务中无特征节点的特征表示

无特征节点的特征表示 节点度数degree pagerank 以pagerank起源的应用场景为例&#xff0c;不是所有的网站都是同等重要的&#xff0c;所以需要根据结构信息对节点进行排序。 直觉上&#xff0c;如果一个网站它有很多链接&#xff0c;它就很重要&#xff0c;举例来说&#…

2023 年 5 大网络攻击

您是否知道 2023 年全球数据泄露的平均损失为 445 万美元&#xff1f;数量不只是数字&#xff1b;它反映了网络威胁对全球经济的深层次影响。他们强调了一个严峻的事实&#xff1a;没有任何实体能够幸免,甚至政府也不能幸免。 在前所未有的技术飞跃中&#xff0c;2023 年网络威…

听GPT 讲Rust源代码--src/tools(16)

File: rust/src/tools/rust-analyzer/crates/ide-completion/src/completions/use_.rs rust-analyzer是一个基于Rust语言的IntelliSense引擎&#xff0c;用于提供IDE自动补全、代码导航和其他代码编辑功能。在rust-analyzer的源代码中&#xff0c;rust/src/tools/rust-analyzer…

用户管理第2节课-idea 2023.2 后端--删除表,从零开始

一、鱼皮清空model文件夹下 二、鱼皮清空mapper文件夹下 三、删除 test 测试类下的部分代码 3.1删除SampleTest 3.2删除部分代码 UserCenterApplicationTests

加密后的数据该如何支持模糊查询

加密后的数据该如何支持模糊查询 在日常工作中&#xff0c;我们经常会有一些模糊查询的条件&#xff0c;比如说按照手机号模糊查询&#xff0c;或者是身份证号码。正常情况下我们可以使用 select * from user where mobile like %123% 来模糊查询&#xff0c;但是这种方式是…

〖大前端 - 基础入门三大核心之JS篇(58)〗- 面向对象案例

说明&#xff1a;该文属于 大前端全栈架构白宝书专栏&#xff0c;目前阶段免费&#xff0c;如需要项目实战或者是体系化资源&#xff0c;文末名片加V&#xff01;作者&#xff1a;哈哥撩编程&#xff0c;十余年工作经验, 从事过全栈研发、产品经理等工作&#xff0c;目前在公司…

【深度学习】序列生成模型(五):评价方法计算实例:计算BLEU-N得分【理论到程序】

文章目录 一、BLEU-N得分&#xff08;Bilingual Evaluation Understudy&#xff09;1. 定义2. 计算N1N2BLEU-N 得分 3. 程序 给定一个生成序列“The cat sat on the mat”和两个参考序列“The cat is on the mat”“The bird sat on the bush”分别计算BLEU-N和ROUGE-N得分(N1或…

uniapp运行到手机模拟器

第一步&#xff0c;下载MUMU模拟器 下载地址&#xff1a;MuMu模拟器官网_安卓12模拟器_网易手游模拟器 (163.com) 第二步&#xff0c;运行mumu模拟器 第三步&#xff0c;运行mumu多开器 第三步&#xff0c;查看abs 端口 第四步&#xff0c;打开HBuilder,如下图&#xff0c;将…

探索 Vuex 的世界:状态管理的新视角(下)

&#x1f90d; 前端开发工程师&#xff08;主业&#xff09;、技术博主&#xff08;副业&#xff09;、已过CET6 &#x1f368; 阿珊和她的猫_CSDN个人主页 &#x1f560; 牛客高级专题作者、在牛客打造高质量专栏《前端面试必备》 &#x1f35a; 蓝桥云课签约作者、已在蓝桥云…

大模型赋能“AI+电商”,景联文科技提供高质量电商场景数据

据新闻报道&#xff0c;阿里巴巴旗下淘天集团和国际数字商业集团都已建立完整的AI团队。 淘天集团已经推出模特图智能生成、官方客服机器人、万相台无界版等AI工具&#xff0c;训练出了自己的大模型产品 “星辰”&#xff1b; 阿里国际商业集团已成立AI Business&#xff0c;…

亚马逊云科技-如何缩容/减小您的AWS EC2根卷大小-简明教程

一、背景 Amazon EBS提供了块级存储卷以用于 EC2 实例&#xff0c;EBS具备弹性的特点&#xff0c;可以动态的增加容量、更改卷类型以及修改预配置的IOPS值。但是EBS不能动态的减少容量&#xff0c;在实际使用中&#xff0c;用户也许会存在此类场景&#xff1a; 在创建AWS EC2…

mac电脑安装虚拟机教程

1、准备一台虚拟机&#xff0c;安装CentOS7 常用的虚拟化软件有两种&#xff1a; VirtualBoxVMware 这里我们使用VirtualBox来安装虚拟机&#xff0c;下载地址&#xff1a;Downloads – Oracle VM VirtualBox 001 点击安装 002 报错&#xff1a;he installer has detected an…

Linux shell编程学习笔记37:readarray命令和mapfile命令

目录 0 前言1 readarray命令的格式和功能 1.1 命令格式1.2 命令功能1.3 注意事项2 命令应用实例 2.1 从标准输入读取数据时不指定数组名&#xff0c;则数据会保存到MAPFILE数组中2.2 从标准输入读取数据并存储到指定的数组2.3 使用 -O 选项指定起始下标2.4 用-n指定有效行数…

【Filament】绘制圆形

1 前言 Filament环境搭建中介绍了 Filament 的 Windows 和 Android 环境搭&#xff0c;绘制三角形中介绍了绘制纯色和彩色三角形&#xff0c;绘制矩形中介绍了绘制纯色和彩色矩形&#xff0c;本文将使用 Filament 绘制圆形。 2 绘制圆形 本文项目结构如下&#xff0c;完整代码…

Apache Flink(十七):Flink On Standalone任务提交-Standalone Application模式

🏡 个人主页:IT贫道_大数据OLAP体系技术栈,Apache Doris,Clickhouse 技术-CSDN博客 🚩 私聊博主:加入大数据技术讨论群聊,获取更多大数据资料。 🔔 博主个人B栈地址:豹哥教你大数据的个人空间-豹哥教你大数据个人主页-哔哩哔哩视频 目录

从零开始的神经网络

先决条件 在本文中&#xff0c;我将解释如何通过实现前向和后向传递&#xff08;反向传播&#xff09;来构建基本的深度神经网络。这需要一些关于神经网络功能的具体知识。 了解线性代数的基础知识也很重要&#xff0c;这样才能理解我为什么要在本文中执行某些运算。我最好的…

【FLV】文件解析源码分析:视频解析为可解码的nalu单元

https flv 拉到的数据是flv宏观看 : 每一部分都是 A+ Prev 的模式 A 可以是header :9个字节可以是TAG :大小可变而每个TAG 都有个固定的部分: TAG HEADER ,大小9个字节 ,里面是类型、大小、时间戳、扩展时间戳、流ID 因此,可以直接去掉9+4 个字节的第一部分:FLV HEADER…