Bo's Oracle Station

查看: 884|回复: 0

ANSIBLE12(force_handlers)

[复制链接]

1005

主题

1469

帖子

1万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
12012
发表于 2020-8-12 22:00:22 | 显示全部楼层 |阅读模式
  1. ---
  2. - name: force handlers
  3.   hosts: server2.example.com
  4.   force_handlers: yes
  5.   tasks:
  6.   - name: a
  7.     shell: "/bin/true"
  8.     notify: h_shell

  9.   - name: a shell
  10.     yum:
  11.       name: notapkg
  12.       state: latest

  13.   handlers:
  14.   - name: h_shell
  15.     service:
  16.       name: mariadb
  17.       state: restarted
复制代码
注释和未注释handlers的输出差异:
[student@classroom ansible]$ ansible-playbook  v6-4_server2.yml

PLAY [force handlers] ****************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [server2.example.com]

TASK [a] *****************************************************************************************************************************************************************
fatal: [server2.example.com]: FAILED! => {"changed": true, "cmd": "/bin/true1", "delta": "0:00:00.006282", "end": "2020-08-12 22:03:19.083473", "msg": "non-zero return code", "rc": 127, "start": "2020-08-12 22:03:19.077191", "stderr": "/bin/sh: /bin/true1: No such file or directory", "stderr_lines": ["/bin/sh: /bin/true1: No such file or directory"], "stdout": "", "stdout_lines": []}

PLAY RECAP ***************************************************************************************************************************************************************
server2.example.com        : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

以上是把/bin/true改成/bin/true1,仍然保留force_handlers,但是不触发handler



[student@classroom ansible]$ ansible-playbook  v6-4_server2.yml

PLAY [force handlers] ****************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [server2.example.com]

TASK [a] *****************************************************************************************************************************************************************
changed: [server2.example.com]
shell模块永远都是黄的,changed


TASK [a shell] ***********************************************************************************************************************************************************
fatal: [server2.example.com]: FAILED! => {"changed": false, "failures": ["No package notapkg available."], "msg": "Failed to install some of the specified packages", "rc": 1, "results": []}

RUNNING HANDLER [h_shell] ************************************************************************************************************************************************

PLAY RECAP ***************************************************************************************************************************************************************
server2.example.com        : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0


以上这一段,是没有写force_handlers的(都是/bin/true)。
下面这一段是写了force_handlers的(都是/bin/true)  


[student@classroom ansible]$ ansible-playbook  v6-4_server2.yml

PLAY [force handlers] ****************************************************************************************************************************************************

TASK [Gathering Facts] ***************************************************************************************************************************************************
ok: [server2.example.com]

TASK [a] *****************************************************************************************************************************************************************
changed: [server2.example.com]
shell模块永远都是黄的,changed


TASK [a shell] ***********************************************************************************************************************************************************
fatal: [server2.example.com]: FAILED! => {"changed": false, "failures": ["No package notapkg available."], "msg": "Failed to install some of the specified packages", "rc": 1, "results": []}

RUNNING HANDLER [h_shell] ************************************************************************************************************************************************
changed: [server2.example.com]


PLAY RECAP ***************************************************************************************************************************************************************
server2.example.com        : ok=3    changed=2    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

[student@classroom ansible]$

如果task a(出发handler的那个task)是绿的,即使force_handlers也不会执行handler:


  1. ---
  2. - name: force handlers
  3.   hosts: server2.example.com
  4.   force_handlers: yes
  5.   tasks:
  6.   - name: a
  7.     service:
  8.       name: sshd
  9.       state: started
  10.       enabled: yes
  11.     notify: h_shell

  12.   - name: a shell
  13.     yum:
  14.       name: notapkg
  15.       state: latest

  16.   handlers:
  17.   - name: h_shell
  18.     service:
  19.       name: mariadb
  20.       state: restarted
复制代码

[student@classroom ansible]$ ansible-playbook  v6-4_server2.yml

PLAY [force handlers] ******************************************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************************************
ok: [server2.example.com]

TASK [a] *******************************************************************************************************************************************************************************
ok: [server2.example.com]

TASK [a shell] *************************************************************************************************************************************************************************
fatal: [server2.example.com]: FAILED! => {"changed": false, "failures": ["No package notapkg available."], "msg": "Failed to install some of the specified packages", "rc": 1, "results": []}

PLAY RECAP *****************************************************************************************************************************************************************************
server2.example.com        : ok=2    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|Bo's Oracle Station   

GMT+8, 2024-5-9 23:21 , Processed in 0.129275 second(s), 24 queries .

快速回复 返回顶部 返回列表