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. ...
随机推荐
- C++设计模式 - 抽象工厂(Abstract Factory)
对象创建模式 通过"对象创建" 模式绕开new,来避免对象创建(new)过程中所导致的紧耦合(依赖具体类),从而支持对象创建的稳定.它是接口抽象之后的第一步工作. 典型模式 Fac ...
- #期望,树的直径#51nod 1803 森林直径
题目 有一棵 \(n\) 个结点的树,按顺序给出树边 \((fa[i],i)\), \(Q\) 次询问查询如果只选取第 \([l,r]\) 条树边,问森林的直径 \(fa[i]\) 的生成方式为 \( ...
- #裴蜀定理#洛谷 2520 [HAOI2011]向量
题目 分析 首先若 \(a,b\) 都为 0 要特判. 若 \(\begin{cases}x=pa+qb+p'a+q'b\\y=qa+pb-q'a-p'b\end{cases}\) 合并同类项可以得到 ...
- #扩展域并查集,线段树分治#CF576E Painting Edges
题目链接 题目翻译 给定一张 \(n\) 个点 \(m\) 条边的无向图. 一共有 \(k\) 种颜色,一开始,每条边都没有颜色. 定义合法状态为仅保留染成 \(k\) 种颜色中的任何一种颜色的边,图 ...
- 今晚19:00知识赋能第2期直播丨OpenHarmony智能家居项目之控制面板界面设计
OpenAtom OpenHarmony(以下简称"OpenHarmony")开源开发者成长计划项目自 2021 年 10 月 24 日上线以来,在开发者中引发高度关注. 成长计划 ...
- Nacos 无法注册服务
情况描述: Nacos服务搭建完成. 引入了Nacos的依赖. <!-- SpringCloud Alibaba 服务管理 --> <dependency> <group ...
- vs报错:RC1004 unexpected end of file found
如图,在编译代码时,出现报错:RC1004 unexpected end of file found 原因是,cpp最后要多一行才行,不然就会报这个错误 错误示例: int main() { ret ...
- Python设计模式----4.构建者模式
构建者模式: 将一个复杂对象的构造与表现进行分离,利用多个步骤进行创建,同一个构建过程可用于创建多个不同的表现 构建者模式一般由 Director(指挥官)和 Builder(建设者)构成 class ...
- 部署javaweb项目到阿里云ecs(centos7)
阿里云文档 https://help.aliyun.com/document_detail/51376.html?spm=5176.12901015.0.i12901015.af8f525cCPi8Q ...
- Spring-Cloud 组件之 Spring Cloud Eureka:服务注册与发现
Spring Cloud Eureka:服务注册与发现 SpringCloud学习教程 SpringCloud Spring Cloud Eureka是Spring Cloud Netflix 子项目 ...