如何在Ansible中执行任务之前强制处理程序运行?

时间:2015-12-01 11:07:39

标签: ansible ansible-handlers

我有一个应该在指定的IP上配置的剧本,而不是连接到这个应用程序来配置内部的东西。

我遇到了一个问题:我在app config中更改了任何内容之后需要重新启动应用程序,如果我没有重新启动应用程序,则连接失败(没有连接,因为应用程序对新IP配置一无所知)地址我正试图访问)。

我目前的剧本:

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已更改,我需要强制执行处理程序。

1 个答案:

答案 0 :(得分:81)

如果你想强制处理程序在两个任务之间而不是在游戏结束时运行,你需要将它放在两个任务之间:

- meta: flush_handlers

来自ansible documentation的示例:

tasks:
   - shell: some tasks go here
   - meta: flush_handlers
   - shell: some other tasks

请注意,这会导致所有挂起的处理程序在此时运行,而不仅仅是那个特定的处理程序。

相关问题