为什么通知任务没有在 ansible 中执行?
Why notify tasks aren't getting executed in ansible?
我面临的问题很常见,但其他解决方案对我不起作用。正如问题所暗示的,当我 运行 我的剧本时,只有第一个通知处理程序被执行。即只有 firewalld 重新启动,但未更新 bash 配置文件。
有些人建议使用通知链接,但我不想合并两个目标完全不同的任务。例如,一项任务可能是将端口添加到 firewalld,然后重新启动它;另一个可能是更新我的 bash 配置文件以显示带有 history
命令输出的日期。
N.B。上面的代码片段不是我的完整 .yml,只是其中的一部分,因此这可能有效也可能无效。但是,原始文件确实有效。
---
tasks
- name: add port-80 to firewalld
firewalld: zone=drop port=80/tcp permanent=true state=enabled
- name: add port-443 to firewalld
firewalld: zone=drop port=443/tcp permanent=true state=enabled
- shell: firewall-cmd --reload
notify:
- Restart firewalld
- name: Enabling time display in history
blockinfile:
dest: ~/.bash_profile
block: |
export HISTTIMEFORMAT="%d/%m/%y %T "
notify:
- Source the updated Bash profile
handlers:
- name: Restart firewalld
service: name=firewalld state=restarted
- name: Source the updated Bash profile
shell: source ~/.bash_profile
...
终于在评论的帮助下解决了问题
The notify handler will be executed if and only if state is changed
as a result of the task.
我犯的错误是试图安装一个已经安装的包。所以,状态没有改变。
举个例子,
---
- hosts: varnish
remote_user: root
tasks:
- name: Install tree
yum: name=tree state=absent
notify:
- Do an echo
handlers:
- name: Do an echo
debug: msg="This will be printed"
...
我们将运行剧本翻两遍。
先运行之后,我们会得到:
TASK [Install tree] ************************************************************
changed: [192.***.***.**]
RUNNING HANDLER [Do an echo] ***************************************************
ok: [192.***.***.**] => {
"msg": "This will be printed"
}
PLAY RECAP *********************************************************************
192.***.***.** : ok=1 changed=1 unreachable=0 failed=0
随着状态的改变(查看 changed=1
部分),将打印调试消息。
第二个运行之后,我们将得到:
TASK [Install tree] ************************************************************
ok: [192.***.***.**]
PLAY RECAP *********************************************************************
192.***.***.** : ok=0 changed=0 unreachable=0 failed=0
要注意的是处理程序不像第一个 运行 那样被调用,因为在第二个 运行 中状态没有改变(树已经安装)。
我已经贴出来作为回答了,希望对各位网友有所帮助。
我面临的问题很常见,但其他解决方案对我不起作用。正如问题所暗示的,当我 运行 我的剧本时,只有第一个通知处理程序被执行。即只有 firewalld 重新启动,但未更新 bash 配置文件。
有些人建议使用通知链接,但我不想合并两个目标完全不同的任务。例如,一项任务可能是将端口添加到 firewalld,然后重新启动它;另一个可能是更新我的 bash 配置文件以显示带有 history
命令输出的日期。
N.B。上面的代码片段不是我的完整 .yml,只是其中的一部分,因此这可能有效也可能无效。但是,原始文件确实有效。
---
tasks
- name: add port-80 to firewalld
firewalld: zone=drop port=80/tcp permanent=true state=enabled
- name: add port-443 to firewalld
firewalld: zone=drop port=443/tcp permanent=true state=enabled
- shell: firewall-cmd --reload
notify:
- Restart firewalld
- name: Enabling time display in history
blockinfile:
dest: ~/.bash_profile
block: |
export HISTTIMEFORMAT="%d/%m/%y %T "
notify:
- Source the updated Bash profile
handlers:
- name: Restart firewalld
service: name=firewalld state=restarted
- name: Source the updated Bash profile
shell: source ~/.bash_profile
...
终于在评论的帮助下解决了问题
The notify handler will be executed if and only if state is
changed
as a result of the task.
我犯的错误是试图安装一个已经安装的包。所以,状态没有改变。
举个例子,
---
- hosts: varnish
remote_user: root
tasks:
- name: Install tree
yum: name=tree state=absent
notify:
- Do an echo
handlers:
- name: Do an echo
debug: msg="This will be printed"
...
我们将运行剧本翻两遍。
先运行之后,我们会得到:
TASK [Install tree] ************************************************************
changed: [192.***.***.**]
RUNNING HANDLER [Do an echo] ***************************************************
ok: [192.***.***.**] => {
"msg": "This will be printed"
}
PLAY RECAP *********************************************************************
192.***.***.** : ok=1 changed=1 unreachable=0 failed=0
随着状态的改变(查看 changed=1
部分),将打印调试消息。
第二个运行之后,我们将得到:
TASK [Install tree] ************************************************************
ok: [192.***.***.**]
PLAY RECAP *********************************************************************
192.***.***.** : ok=0 changed=0 unreachable=0 failed=0
要注意的是处理程序不像第一个 运行 那样被调用,因为在第二个 运行 中状态没有改变(树已经安装)。
我已经贴出来作为回答了,希望对各位网友有所帮助。