ansible系列(23)--ansible的when控制语句
1 when控制语句
when 判断在用于控制在满足when所指定的条件的情况下才执行响应的动作。
使用场景:
- 比如:
web节点都需要配置nginx仓库,但其他节点并不需要,此时就会用到when判断。 - 比如:
Centos与Ubuntu都需要安装Apache,而Centos系统软件包为httpd,而Ubuntu系统软件包为httpd2,那么此时就需要判断主机系统,然后为不同的主机系统安装不同的软件包。
1.1 根据不同操作系统安装相同的软件
需求:为所有主机安装 Apache 软件,若系统为 CentOS :安装 httpd,若系统为 Ubuntu :安装 httpd2。
ansible_distribution变量可以获取到主机的发行版本。
playbook编写如下:
[root@xuzhichao playbook]# cat when1.yml
- hosts: webs
remote_user: root tasks:
- name: CentOS Install httpd
yum:
name: httpd
state: present
when: ansible_distribution == "CentOS" <==判断版本语句,此处变量不需要{{ }}引用。 - name: Ubuntu Install httpd
yum:
name: httpd2
state: present
when: ansible_distribution == "Ubuntu"
主机hosts文件如下:
[root@xuzhichao playbook]# tail /etc/ansible/hosts
[webs]
192.168.20.22
192.168.20.23
执行结果:
[root@xuzhichao playbook]# ansible-playbook when1.yml PLAY [webs] *************************************************************************************************************************************************** TASK [CentOS Install httpd] ***********************************************************************************************************************************
ok: [192.168.20.22]
ok: [192.168.20.23] TASK [Ubuntu Install httpd] ***********************************************************************************************************************************
skipping: [192.168.20.22] <==跳过执行此任务
skipping: [192.168.20.23] PLAY RECAP ****************************************************************************************************************************************************
192.168.20.22 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
192.168.20.23 : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
1.2 为不同centos版本安装httpd软件
centos6和centos7中的httpd服务,版本不同,配置文件也不相同,此时可以将模板根据centos的版本发送到指定的目标主机。
ansible_distribution_major_version变量用于判断centos主版本号。
playbook文件如下:
---
- hosts: web
remote_user: root tasks:
- name: install package
yum:
name: httpd - name: copy template centos6
template:
src: httpd_6.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart service
when: ansible_distribution_major_version == "6" <==判断版本语句,此处变量不需要{{ }}引用。 - name: copy template centos7
template:
src: httpd_7.conf.j2
dest: /etc/httpd/conf/httpd.conf
notify: restart service
when: ansible_distribution_major_version == "7" <==判断版本语句 - name: start service
service:
name: httpd
state: started handlers:
- name: restart service
service:
name: httpd
state: restarted
1.3 为特定的主机添加Nginx仓库
为特定的主机添加 Nginx 仓库:主机名为 web 则添加 Nginx 仓库,主机名不为 web 则不做任何处理。
编写playbook文件:
[root@xuzhichao playbook]# cat when2.yml
- hosts: all
remote_user: root
tasks:
- name: Add Nginx Yum Repository
yum_repository:
name: nginx
description: Nginx Repository
baseurl: http://nginx.org/packages/centos/7/$basearch/
gpgcheck: no
when: (ansible_hostname is match("web*")) or (ansible_hostname is match("nginx*")
#when也可以使用and与or方式进行多项匹配。
运行playbook:
[root@xuzhichao playbook]# ansible-playbook when2.yml
查看运行效果:
[root@nginx03 ~]# cat /etc/yum.repos.d/nginx.repo
[nginx]
baseurl = http://nginx.org/packages/centos/7/$basearch/
gpgcheck = 0
name = Nginx Repository
1.4 判断服务是否正常运行
判断 nginx 服务是否处于运行状态,已运行:则重启服务;未运行:则不做处理。
playbook文件如下:
[root@xuzhichao playbook]# cat when3.yml
- hosts: NginxWebs
remote_user: root
tasks:
- name: Check Nginx Status
shell:
cmd: systemctl is-active nginx
ignore_errors: yes
register: check_nginx
- name: Print Check_nginx
debug:
var:
"check_nginx" <==#通过debug的var输出该变量的所有内容
- name: Nginx Restart
service:
name: nginx
state: restarted
when: check_nginx.rc == 0 <==.rc是check_nginx变量中的执行结果,见下面的执行过程
执行playbook:
#其中192.168.20.22主机nginx检测失败,没有重启服务;
#192.168.20.23主机nginx检测成功,重启服务;
[root@xuzhichao playbook]# ansible-playbook when3.yml
PLAY [NginxWebs] **********************************************************************************************************************************************
TASK [Check Nginx Status] *************************************************************************************************************************************
fatal: [192.168.20.22]: FAILED! => {"changed": true, "cmd": "systemctl is-active nginx", "delta": "0:00:00.007774", "end": "2021-08-04 18:33:05.331828", "msg": "non-zero return code", "rc": 3, "start": "2021-08-04 18:33:05.324054", "stderr": "", "stderr_lines": [], "stdout": "unknown", "stdout_lines": ["unknown"]}
...ignoring
changed: [192.168.20.23]
TASK [Print Check_nginx] **************************************************************************************************************************************
ok: [192.168.20.22] => {
"check_nginx": {
"changed": true,
"cmd": "systemctl is-active nginx",
"delta": "0:00:00.007774",
"end": "2021-08-04 18:33:05.331828",
"failed": true,
"msg": "non-zero return code",
"rc": 3, <==.rc是check_nginx变量中的执行结果状态
"start": "2021-08-04 18:33:05.324054",
"stderr": "",
"stderr_lines": [],
"stdout": "unknown",
"stdout_lines": [
"unknown" <==执行结果。
]
}
}
ok: [192.168.20.23] => {
"check_nginx": {
"changed": true,
"cmd": "systemctl is-active nginx",
"delta": "0:00:00.007241",
"end": "2021-08-04 18:33:05.331485",
"failed": false,
"rc": 0, <==.rc是check_nginx变量中的执行结果状态,0表示执行正常
"start": "2021-08-04 18:33:05.324244",
"stderr": "",
"stderr_lines": [],
"stdout": "active",
"stdout_lines": [
"active" <==执行结果。
]
}
}
TASK [Nginx Restart] ******************************************************************************************************************************************
skipping: [192.168.20.22]
changed: [192.168.20.23]
PLAY RECAP ****************************************************************************************************************************************************
192.168.20.22 : ok=2 changed=1 unreachable=0 failed=0 skipped=1 rescued=0 ignored=1
192.168.20.23 : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible系列(23)--ansible的when控制语句的更多相关文章
- Ansible系列(二):选项和常用模块
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Ansible系列(一):基本配置和使用
本文目录:1.1 安装Ansible1.2 配置Ansible 1.2.1 环境配置 1.2.2 SSH互信配置 1.2.3 简单测试1.3 inventory Ansible是一种批量.自动部署工具 ...
- Ansible系列(五):playbook应用和roles自动化批量安装示例
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Ansible系列(六):各种变量定义方式和变量引用
本文目录:1.1 ansible facts1.2 变量引用json数据的方式 1.2.1 引用json字典数据的方式 1.2.2 引用json数组数据的方式 1.2.3 引用facts数据1.3 设 ...
- Ansible系列(三):YAML语法和playbook写法
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- Ansible系列(六):循环和条件判断
本文目录:1. 循环 1.1 with_items迭代列表 1.2 with_dict迭代字典项 1.3 with_fileglob迭代文件 1.4 with_lines迭代行 1.5 with_ne ...
- Ansible系列(七):执行过程分析、异步模式和速度优化
本文目录:1.1 ansible执行过程分析1.2 ansible并发和异步1.3 ansible的-t选项妙用1.4 优化ansible速度 1.4.1 设置ansible开启ssh长连接 1.4. ...
- Ansible系列(四):playbook应用和roles自动化批量安装示例
Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html playbook是ansible实现批量自动化最重要的手段.在其中可以使用变 ...
- Ansible系列(五):各种变量定义方式和变量引用
Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.1 ansible facts facts组件是用来收集被管理节点信息的 ...
- 自动化运维工具ansible学习+使用ansible批量推送公钥到远程主机
目录: 一.ansible简介 1.1.ansible是什么 1.2.ansible如何工作 1.3.ansible优缺点 1.4.ansible安装方式 1.5.ansible文件简单介绍 1.6. ...
随机推荐
- 数据库锁起来了,把事务清掉sql
select concat('kill ',id,';') from information_schema.`PROCESSLIST` where state !='executing' 将上述代码执 ...
- 使用OHOS SDK构建assimp
参照OHOS IDE和SDK的安装方法配置好开发环境. 从github下载源码. 执行如下命令: git clone https://github.com/assimp/assimp.git 进入源码 ...
- OpenHarmony社区运营报告(2022年9月)
篇首语 在开放原子开源基金会.OpenAtom OpenHarmony(简称"OpenHarmony")工作委员会.会员及共建单位和开发者的共同努力下,OpenHarmony在技术 ...
- Python 集合(Sets)2
访问项 您无法通过引用索引或键来访问集合中的项.但是,您可以使用for循环遍历集合项,或者使用in关键字检查集合中是否存在指定的值. 示例,遍历集合并打印值: thisset = {"app ...
- Qt 设置图标的三种方式
Qt 设置软件窗口图标有三种方式: 一.通过资源文件,设置图标 this->setWindowIcon(QIcon(":/logo.ico")); 二.通过 pro 文件,设 ...
- easyExcel合并数据导出(一对多)
语言 java 框架 ssm 需求 :看图 也是导出效果 数据库查询为(关系为一对多) 一个学生对应多个课程 实现步骤 1.实体类配置, 建议单独写个实体用来导出使用() 学生信息字段正常配置 , ...
- .gitignore 基础
前言 gitignore文件有几种方式生成. 1.在创建版本库的时候生成. 2.自己手动添加: windows环境下,不能把文件直接改名为.gitignore,会提示你必须输入文件名.所以我们先新建一 ...
- Visual Studio 2019汇编报错 warning LNK4258: 指令“/ENTRY:main@0”与开关“/ENTRY:main”不兼容;已忽略
Visual Studio 2019汇编报错 warning LNK4258: 指令"/ENTRY:main@0"与开关"/ENTRY:main"不兼容:已忽略 ...
- wordcloud 词云Python
from wordcloud import WordCloud import matplotlib.pyplot as plt def get_word_cloud(words_list): #首先实 ...
- 使用ollama分别在我的window、mac、小米手机上部署体验llama3-8b
1.ollama到底是个什么玩意 一句话来说, Ollama 是一个基于 Go 语言开发的简单易用的本地大模型运行框架.可以将其类比为 docker(有类似docker中的一些常规命令list,pul ...