ansible系列(23)--ansible的when控制语句



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 
posted @ 2021-08-18 17:29  向往自由的独行者  阅读(1082)  评论(0编辑  收藏  举报