公司动态

handler中添加when判断

📅 2026/7/19 4:21:20
handler中添加when判断
handler中添加上when判断当条件表达式不成立时handler也不会被触发。编写playbook文件rootmaster:/data00/ansible# cat handler7.yamlname: handler使用hosts: dbvars:env: prodtasks:name: copy test1文件copy:src: /data00/ansible/test1.txtdest: /tmp/test1.txtmode: 600notify: handler3handlers:name: notify handlerdebug:msg: “handler触发了”when: env is defined and env “dev”name: handler2debug:msg: “handler2触发了”when: env is defined and env “test”name: handler3debug:msg: “handler3触发了”when: env is defined and env “prod”执行发现只有handler3被触发了rootmaster:/data00/ansible# ansible-playbook handler7.yamlPLAY [handler使用] ***********************************************************************************************************************************************************************************************TASK [Gathering Facts] *******************************************************************************************************************************************************************************************ok: [10.37.99.63]TASK [copy test1文件] ********************************************************************************************************************************************************************************************ok: [10.37.99.63]PLAY RECAP *******************************************************************************************************************************************************************************************************10.37.99.63 : ok2 changed0 unreachable0 failed0 skipped0 rescued0 ignored0rootmaster:/data00/ansible# echo 222 test1.txtrootmaster:/data00/ansible# ansible-playbook handler7.yamlPLAY [handler使用] ***********************************************************************************************************************************************************************************************TASK [Gathering Facts] *******************************************************************************************************************************************************************************************ok: [10.37.99.63]TASK [copy test1文件] ********************************************************************************************************************************************************************************************changed: [10.37.99.63]RUNNING HANDLER [handler3] ***************************************************************************************************************************************************************************************ok: [10.37.99.63] {“msg”: “handler3触发了”}PLAY RECAP *******************************************************************************************************************************************************************************************************10.37.99.63 : ok3 changed1 unreachable0 failed0 skipped0 rescued0 ignored0回到顶部总结handler常见坑点handler name 必须完全匹配 notify 字符串大小写、空格敏感