如何在Ansible中执行任务之前强制处理程序运行? [英] How to force handler to run before executing a task in Ansible?
问题描述
我有一个剧本,应该在指定的IP上进行配置,然后连接到此应用以在内部配置内容.
I have a playbook which should configure on specified IP, and than connect to this app to configure stuff inside.
我遇到了一个问题:更改了应用程序配置中的任何内容后,我需要重新启动应用程序,如果不重新启动应用程序,则无法连接到该应用程序(无连接,因为应用程序对使用新IP的新配置一无所知我正在尝试访问的地址).
I've got a problem: I need to restart app after I've changed anything in app config, and if I do not restart app, connection to it failed (no connection because app knows nothing about new config with new IP address I'm trying to access).
我当前的剧本:
tasks:
- name: Configure app
template: src=app.conf.j2 dest=/etc/app.conf
notify: restart app
- name: Change data in app
configure_app: host={{new_ip}} data={{data}}
handlers:
- name: restart app
service: name=app state=restarted
如果在执行更改应用程序中的数据"之前更改了configure_app
,我需要强制处理程序运行.
I need to force the handler to run if configure_app
changed before executing 'Change data in app'.
推荐答案
如果要强制处理程序在两个任务之间而不是在播放结束时运行,则需要将此处理程序放在两个任务之间:
If you want to force the handler to run in between the two tasks instead of at the end of the play, you need to put this between the two tasks:
- meta: flush_handlers
示例摘自ansible 文档 :
Example taken from the ansible documentation :
tasks:
- shell: some tasks go here
- meta: flush_handlers
- shell: some other tasks
请注意,这将导致所有待处理的处理程序在该时刻运行,而不仅仅是特定的处理程序.
Note that this will cause all pending handlers to run at that point, not just that specific one.
这篇关于如何在Ansible中执行任务之前强制处理程序运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!