我面临的问题很普遍,但是其他的解决方案对我来说并不管用。如问题所示,当我运行我的剧本时,只有第一个通知处理程序才会被执行。也就是说,只有firewalld重新启动,但是更新的bash配置文件没有来源。
有些人建议通知链接,但我不想合并两个任务与完全不同的目标。例如,一项任务可能是向firewalld添加端口,然后重新启动;另一项任务可以更新bash以使用history命令输出显示日期。
注:上面的片段不是我的完整.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
...发布于 2016-05-18 19:35:34
最后,我在评论的帮助下解决了这个问题。
如果状态是任务的结果,状态为
changed,则通知处理程序将执行。
我所做的错误是试图安装一个已经安装的软件包。所以州并没有改变。
让我们举个例子来看看,
---
- 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 与第一次运行不同的是,要查看的是处理程序,因为状态在第二次运行时不会更改(树已经安装)。
我已经张贴它作为一个答案,它可能会帮助其他用户。
https://stackoverflow.com/questions/37292744
复制相似问题