如何在 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:
- 元: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屋!