1 when控制语句

when 判断在用于控制在满足when所指定的条件的情况下才执行响应的动作。

使用场景:

  • 比如: web 节点都需要配置 nginx 仓库,但其他节点并不需要,此时就会用到when 判断。
  • 比如: CentosUbuntu 都需要安装 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软件

centos6centos7中的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控制语句的更多相关文章

  1. Ansible系列(二):选项和常用模块

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  2. Ansible系列(一):基本配置和使用

    本文目录:1.1 安装Ansible1.2 配置Ansible 1.2.1 环境配置 1.2.2 SSH互信配置 1.2.3 简单测试1.3 inventory Ansible是一种批量.自动部署工具 ...

  3. Ansible系列(五):playbook应用和roles自动化批量安装示例

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  4. Ansible系列(六):各种变量定义方式和变量引用

    本文目录:1.1 ansible facts1.2 变量引用json数据的方式 1.2.1 引用json字典数据的方式 1.2.2 引用json数组数据的方式 1.2.3 引用facts数据1.3 设 ...

  5. Ansible系列(三):YAML语法和playbook写法

    html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...

  6. Ansible系列(六):循环和条件判断

    本文目录:1. 循环 1.1 with_items迭代列表 1.2 with_dict迭代字典项 1.3 with_fileglob迭代文件 1.4 with_lines迭代行 1.5 with_ne ...

  7. Ansible系列(七):执行过程分析、异步模式和速度优化

    本文目录:1.1 ansible执行过程分析1.2 ansible并发和异步1.3 ansible的-t选项妙用1.4 优化ansible速度 1.4.1 设置ansible开启ssh长连接 1.4. ...

  8. Ansible系列(四):playbook应用和roles自动化批量安装示例

    Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html playbook是ansible实现批量自动化最重要的手段.在其中可以使用变 ...

  9. Ansible系列(五):各种变量定义方式和变量引用

    Ansible系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.html 1.1 ansible facts facts组件是用来收集被管理节点信息的 ...

  10. 自动化运维工具ansible学习+使用ansible批量推送公钥到远程主机

    目录: 一.ansible简介 1.1.ansible是什么 1.2.ansible如何工作 1.3.ansible优缺点 1.4.ansible安装方式 1.5.ansible文件简单介绍 1.6. ...

随机推荐

  1. Java字符串比较 == 和 equals方法的区别

    今天在排除一个bug的时候出现了一个很低级但是也很容易被忽视的错误,在此写了一个小例子做记录. 首先我先说一下错误的场景,我读取了一段json数据,并使用JSONObject的实例对象的getStri ...

  2. Scala 不可变集合Set

    1 package chapter07 2 3 object Test06_ImmutableSet { 4 def main(args: Array[String]): Unit = { 5 // ...

  3. #树形dp#B 预算缩减

    题目 给定一棵树,你需要删去一些边(可以不删),使得剩下的图中每个点所在的连通块大小都\(\geq k\). 求删边的方案数,对\(786433\)取模.两种方案不同,当且仅当存在一条边在一个方案中被 ...

  4. 使用 GitLab CI/CD 和阿里云 CLI 自动部署前端项目

    一.什么是 CI/CD? CI(持续交付)是功能迭代后自动构建.打包.校验代码格式.跑单测.单测覆盖率,常见的就是 Webpack.Rollup.ESLint等. CD(持续部署)是经过 CI 后,代 ...

  5. 使用 OpenCV 进行文档矫正

    使用 OpenCV 进行文档矫正 本文只发布于博客园与pchar博客 std::vector<std::vector<cv::Point>> cvhelper::findCor ...

  6. 10. Conclusion

    10.1 Matrix Factorizations A = LU = (Lower triangular L with 1's on the diagonal)(Upper triangular U ...

  7. RabbitMQ 11 死信队列

    死信队列 概述 消息队列中的数据,如果迟迟没有消费者来处理,就会一直占用消息队列的空间. 比如抢车票的场景,用户下单高铁票之后,会进行抢座,然后再进行付款,但是如果用户下单之后并没有及时的付款,这张票 ...

  8. Grafana 系列-统一展示-4-AWS Cloudwatch 数据源

    系列文章 Grafana 系列文章 AWS Cloudwatch 数据源 对于 AWS Cloudwatch, 主要在于 3 种不同的认证方式: AWS SDK Default IAM Role AK ...

  9. 知识图谱增强的KG-RAG框架

    昨天我们聊到KG在RAG中如何发挥作用,今天我们来看一个具体的例子. 我们找到一篇论文: https://arxiv.org/abs/2311.17330 ,论文的研究人员开发了一种名为知识图谱增强的 ...

  10. stack-c++

    #include <iostream> using namespace std; template <typename T> class Stack{ private: int ...