linux学习

Ansible 通过 notify 触发执行 handlers 任务

handlers介绍

当某个任务需要依赖其他任务怎么办?

可以通过handlers定义触发执行的任务,但执行handlers中定义的任务是有条件的:

  • 仅当某个任务触发(notify)handlers时才执行相应的任务

  • 如果有多个notify触发执行handlers任务,也仅执行一次

  • 仅当任务的执行状态为changed时handlers任务才执行

  • handlers任务在所有其他任务都执行后才执行

    通过notify触发执行handlers任务,代码示意如下:

先创建文件夹/tmp/handa/xixi,然后在该文件夹里面创建文件1.txt:

[root@pubserver ansible]# vim handlres.yml
---
- name: handlers test
  hosts: web1
  tasks:
    - name: create dir
      file:                 # 创建文件夹
        path: /tmp/handa/xixi1
        state: directory
      notify: touch file    # 通知touch file 需要执行

  handlers:                 # 执行touche file任务
    - name: touch file
      file: 
        path: /tmp/handa/xixi/1.txt
        touch: touch

# 第一次执行,任务一成功执行,任务二执行失败
[root@pubserver ansible]# ansible-playbook handlres.yml

PLAY [handlers test] ******************************************************************************************************************************

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

TASK [create dir] *********************************************************************************************************************************
changed: [web1]

RUNNING HANDLER [touch file] **********************************************************************************************************************
fatal: [web1]: FAILED! => {"changed": false, "msg": "Unsupported parameters for (file) module: touch Supported parameters include: _diff_peek, _original_basename, access_time, access_time_format, attributes, backup, content, delimiter, directory_mode, follow, force, group, mode, modification_time, modification_time_format, owner, path, recurse, regexp, remote_src, selevel, serole, setype, seuser, src, state, unsafe_writes"}

NO MORE HOSTS LEFT ********************************************************************************************************************************

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

# 第二次执行,任务全部执行成功
[root@pubserver ansible]# ansible-playbook handlres.yml

PLAY [handlers test] ******************************************************************************************************************************

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

TASK [create dir] *********************************************************************************************************************************
ok: [web1]

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

留言

您的邮箱地址不会被公开。 必填项已用 * 标注