实施任务控制

条件任务

when语句用于有条件地运行任务。它取要测试的条件为值。如果条件满足,则运行任务。如果条件不满足,则跳过任务。

[root@localhost ansible]# cat ss.yml 
---
- name: kkkk
  hosts: apache
  vars:
    power: true
  tasks:
    - name: 122
      yum:
        name: httpd
        state: present
      when: power
   
[root@localhost ansible]# ansible-playbook ss.yml 

PLAY [kkkk] *********************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.216.172]

TASK [yum] **********************************************************************************************************************
changed: [192.168.216.172]

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

操作示例
等于(值为字符串)ansible_machine == “x86_64”
等于(值为数字)max_memory == 512
小于min_memory < 128
大于min_memory > 256
小于等于min_memory <= 256
大于等于min_memory >= 512
不等于min_memory != 512
变量存在min_memory is defined
变量不存在min_memory is not defined
布尔变量是True。1、True或yes的求值为True memory_available
布尔变量是False。0、False或no的求值为False not memory_available
第一个变量的值存在,作为第二个变量的列表中的值ansible_distribution in supported_distros

测试多个条件

一个when语句可用于评估多个条件。使用and和or关键字组合条件,并使用括号分组条件。

如果任一条件为真时满足条件语句,则应当使用or语句。

使用and运算时,两个条件都必须为真,才能满足整个条件语句。

对某个任务结合使用when和loop时,将对每个项检查when语句。

ansible 处理程序

处理程序是响应由其他任务触发的通知的任务。仅当任务在受管主机上更改了某些内容时,任务才通知其处理程序。每个处理程序具有全局唯一的名称,在playbook中任务块的末尾触发。如果没有任务通过名称通知处理程序,处理程序就不会运行。如果一个或多个任务通知处理程序,处理程序就会在play中的所有其他任务完成后运行一次。因为处理程序就是任务,所以可以在处理程序中使用他们将用于任何其他任务的模块。通常而言,处理程序被用于重新引导主机和重启服务。

  1. 处理程序始终按照 play 的 handlers 部分指定的顺序运行,不按 notify 里的

  2. 处理程序通常在相关 play
    中所有其他任务运行完后运行
    3. 处理程序名称存在于个 play 命名空间中(如果两个处理程序同名,只会运行一个)

  3. 如果多个任务通知处理程序,处理程序也只会运行一次

  4. 如果包含 notify 的语句任务没有报告 changed
    结果,则处理程序不会获得通知

  - name: 221
      service:
        name: firewalld
        state: started  # 防火墙已提前关闭
      notify:
        - stop firewalld # 通知关闭防火墙
  handlers:
    - name: stop firewalld
      service:   # 调用模块
        name: firewalld
        state: stopped
TASK [service] ******************************************************************************************************************
changed: [192.168.216.172]

RUNNING HANDLER [stop firewalld] ************************************************************************************************
changed: [192.168.216.172]

PLAY RECAP **********************************************************************************************************************
192.168.216.172            : ok=4    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

处理任务失败

管理play中的任务错误

Ansible评估任务的返回代码,从而确定任务是成功还是失败。通常而言,当任务失败时,Ansible将立即在该主机上中止play的其余部分并且跳过所有后续任务。

但有些时候,可能希望即使在任务失败时也继续执行play。

忽略任务失败

默认情况下,任务失败时play会中止。不过,可以通过忽略失败的任务来覆盖此行为。可以在任务中使用ignore_errors关键字来实现此目的。

[root@localhost ansible]# cat useradd.yml 
---
- name: useradd
  hosts: apache
  tasks:
    - name: 11
      user:
        name: 123
        uid: 1 
      ignore_errors: yes  # 此任务失败后依然执行
    - name: 22
      user:
        name: jjj
        uid: 1006
[root@localhost ansible]# ansible-playbook useradd.yml 

PLAY [useradd] ******************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.216.172]

TASK [user] *********************************************************************************************************************
[WARNING]: The value 123 (type int) in a string field was converted to u'123' (type string). If this does not look like what you
expect, quote the entire value to ensure it does not change.
fatal: [192.168.216.172]: FAILED! => {"changed": false, "msg": "useradd:UID 1 并不唯一\n", "name": "123", "rc": 4}
...ignoring

TASK [user] *********************************************************************************************************************
changed: [192.168.216.172]

PLAY RECAP **********************************************************************************************************************
192.168.216.172            : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=1   

任务失败后强制执行处理程序

通常而言,如果任务失败并且play在该主机上中止,则收到play中早前任务通知的处理程序将不会运行。如果在play中设置force_handlers: yes关键字,则即使play因为后续任务失败而中止也会调用被通知的处理程序。

[root@localhost ansible]# cat useradd.yml 
---
- name: useradd
  force_handlers: yes
  hosts: apache
  tasks:
    - name: 24yy2
      user:
        name: abc
        uid: 1011
      notify:
        - jjj  
    - name: 11
      user:
        name: 123
        uid: 1 
  handlers:
    - name: jjj
      service:
        name: httpd
        state: stopped
[root@localhost ansible]# ansible-playbook useradd.yml 

PLAY [useradd] ******************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.216.172]

TASK [24yy2] ********************************************************************************************************************
changed: [192.168.216.172]

TASK [user] *********************************************************************************************************************
[WARNING]: The value 123 (type int) in a string field was converted to u'123' (type string). If this does not look like what you
expect, quote the entire value to ensure it does not change.
fatal: [192.168.216.172]: FAILED! => {"changed": false, "msg": "useradd:UID 1 并不唯一\n", "name": "123", "rc": 4}

RUNNING HANDLER [jjj] ***********************************************************************************************************
changed: [192.168.216.172]

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

处理程序会在任务报告changed结果时获得通知,而在任务报告ok或failed结果时不会获得通知。

指定任务失败条件

可以在任务中使用failed_when关键字来指定表示任务已失败的条件。这通常与命令模块搭配使用,这些模块可能成功执行了某一命令,但命令的输出可能指示了失败。

[root@localhost ansible]# cat useradd.yml 
---
- name: useradd
  force_handlers: yes
  hosts: apache
  tasks:
    - name: 24yy2
      command: date
      register: result

    - name: 3131
      fail: 
        msg: "2021.7.27"
      when: " '2021' in result['stdout' ]"
[root@localhost ansible]# ansible-playbook useradd.yml 

PLAY [useradd] ******************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.216.172]

TASK [24yy2] ********************************************************************************************************************
changed: [192.168.216.172]

TASK [fail] *********************************************************************************************************************
fatal: [192.168.216.172]: FAILED! => {"changed": false, "msg": "2021.7.27"}

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

指定何时任务报告 “Changed” 结果

当任务对托管主机进行了更改时,会报告 changed 状态并通知处理程序。如果任务不需要进行更改,则会报告ok并且不通知处理程序。

changed_when关键字可用于控制任务在何时报告它已进行了更改

changed_when: false,设置后仅报告ok或failed。

[root@localhost ansible]# cat useradd.yml 
---
- name: useradd
  force_handlers: yes
  hosts: apache
  tasks:
    - name: 24yy2
      user:
        name: jjj
      changed_when: "'present' in result.state"
      register: result
      notify:
        - 3131
  handlers:
    - name: 3131
      user:
        name: jjj
        state: absent
[root@localhost ansible]# ansible-playbook useradd.yml 

PLAY [useradd] ******************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.216.172]

TASK [24yy2] ********************************************************************************************************************
changed: [192.168.216.172]
[WARNING]: Failure using method (v2_playbook_on_handler_task_start) in callback plugin
(<ansible.plugins.callback.default.CallbackModule object at 0x7feea6f4d450>): 'int' object has no attribute 'strip'

RUNNING HANDLER [24yy2] *********************************************************************************************************
changed: [192.168.216.172]

PLAY RECAP **********************************************************************************************************************
192.168.216.172            : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

```swift
[root@localhost ~]# id jjjj
id: jjjj: no such user

###  Ansible块和错误处理
在playbook中,块是对任务进行逻辑分组的子句,可用于控制任务的执行方式。例如,任务块可以含有when关键字,以将某一条件应用到多个任务


    block:定义要运行的主要任务
    rescue:定义要在block子句中定义的任务失败时运行的任务
    always:定义始终都独立运行的任务,不论block和rescue子句中定义的任务是成功还是失败


```swift
[root@localhost ansible]# cat ac.yml 
---
- name: apappap
  hosts: apache
  tasks:
  - name: 111
    block:
      - name: 1313
        service: 
          name: 123 # 错误任务
          state: absent
    rescue:
      - name: 12345
        service:
          name: firewalld
          state: started
    always:
      - name: 1231313
        shell: echo " hello world"
[root@localhost ansible]# ansible-playbook ac.yml 

PLAY [apappap] ******************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************
ok: [192.168.216.172]

TASK [service] ******************************************************************************************************************
[WARNING]: The value 123 (type int) in a string field was converted to u'123' (type string). If this does not look like what you
expect, quote the entire value to ensure it does not change.
fatal: [192.168.216.172]: FAILED! => {"changed": false, "msg": "value of state must be one of: reloaded, restarted, started, stopped, got: absent"}

TASK [service] ******************************************************************************************************************
ok: [192.168.216.172]

TASK [shell] ********************************************************************************************************************
changed: [192.168.216.172]

PLAY RECAP **********************************************************************************************************************
192.168.216.172            : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=1    ignored=0   

错误任务后依然执行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值