Ansible-when条件语句

1、when介绍

1.1、作用

when 判断在 Ansible 中的使用频率非常高;比如 yum 模块可以自动检测软件包是否已被安装,而无需人为干涉,但对于有些任务则是需要进行判断才可以实现的。

1.2、场景

比如: web 节点都需要配置 nginx 仓库,但其他节点并不需要,此时就会用到when 判断。
比如: Centos 与 Ubuntu 都需要安装 Apache ,而 Centos 系统软件包为httpd ,而 Ubuntu 系统软件包为 httpd2 ,那么此时就需要判断主机系统,然后为不同的主机系统安装不同的软件包。

2、示例1-根据不同操作系统安装相同的软件

2.1、需求

为所有主机安装 Apache 软件
系统为 CentOS :安装 httpd
系统为 Ubuntu :安装 httpd2

2.2、编写playbook

cat << 'CAT_END' > when_system.yaml
- hosts: httpd
  tasks:
    - name: CentOS Install httpd
      yum: name=httpd state=present
      when: (ansible_distribution=="CentOS")
    - name: Ubuntu Install httpd
      yum: name=httpd state=present
      when: (ansible_distribution=="Ubuntu")
CAT_END

 

2.3、执行 playbook

]# ansible-playbook when_system.yaml 

PLAY [httpd] *************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.10.18]
ok: [192.168.10.17]

TASK [CentOS Install httpd] **********************************************************************************************************************************
ok: [192.168.10.18]
ok: [192.168.10.17]

TASK [Ubuntu Install httpd] # 此处已经忽略
skipping: [192.168.10.17]
skipping: [192.168.10.18]

PLAY RECAP ***************************************************************************************************************************************************
192.168.10.17              : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
192.168.10.18              : ok=2    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0 

3、示例2-为特定的主机添加Nginx仓库

3.1、需求

为所有主机添加 Nginx 仓库
主机名为 web :添加 Nginx 仓库
主机名不为 web :不做任何处理

3.2、编写playbook

cat << 'CAT_END' > when_repo.yaml
- hosts: all
  tasks:
  - name: Add nginx yum repo
    yum_repository:
      name: nginx
      description: nginx repo
      baseurl: http://nginx.org/packages/centos/7/$basearch/
      gpgcheck: no
      enabled: no
    when: ( ansible_hostname is match("http*") )
CAT_END

3.3、执行 playbook

]# ansible-playbook when_repo.yaml 

PLAY [all] ***************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.10.16]
ok: [192.168.10.17]
ok: [192.168.10.18]
ok: [192.168.10.15]

TASK [Add nginx yum repo] ************************************************************************************************************************************
skipping: [192.168.10.15]
skipping: [192.168.10.16]
changed: [192.168.10.18]
changed: [192.168.10.17]

PLAY RECAP ***************************************************************************************************************************************************
192.168.10.15              : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
192.168.10.16              : ok=1    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
192.168.10.17              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
192.168.10.18              : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

4、示例3-判断服务是否正常运行

4.1、需求

判断 httpd 服务是否处于运行状态
已运行:则重启服务
未运行:则不做处理

4.2、编写playbook,通过 register 将命令执行结果保存至变量,然后通过 when 语句进行判断

cat << 'CAT_END' > when_server.yaml
- hosts: httpd
  tasks:
  - name: check nginx server
    command: systemctl is-active nginx
    ignore_errors: yes
    register: check_httpd
  - name: debug print var
    debug: var=check_httpd
  - name: nginx restart
    systemd: name=nginx state=restarted
    when: check_httpd.rc == 0
CAT_END

4.3、执行 playbook

]# ansible-playbook when_server.yaml 

PLAY [httpd] *************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************
ok: [192.168.10.17]
ok: [192.168.10.18]

TASK [check nginx server] ************************************************************************************************************************************
changed: [192.168.10.18]
...
TASK [debug print var] ***************************************************************************************************************************************...
TASK [nginx restart] *****************************************************************************************************************************************
skipping: [192.168.10.17]
changed: [192.168.10.18]

PLAY RECAP ***************************************************************************************************************************************************
192.168.10.17              : ok=3    changed=1    unreachable=0    failed=0    skipped=1    rescued=0    ignored=1   
192.168.10.18              : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

 

posted @ 2023-05-13 16:10  小粉优化大师  阅读(363)  评论(0编辑  收藏  举报