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

77

我有一个playbook,应该在指定的IP上进行配置,然后连接到这个应用程序来配置内部内容。

我的问题是:我需要在更改应用程序配置后重新启动应用程序,如果我不重新启动应用程序,连接将失败(因为应用程序不知道新的配置和我正在尝试访问的新IP地址)。

我的当前playbook:

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个回答

143

如果您想要强制处理程序在两个任务之间而不是在播放结束时运行,您需要将此放置在两个任务之间:

- meta: flush_handlers

本例取自ansible的文档

tasks:
   - shell: some tasks go here
   - meta: flush_handlers
   - shell: some other tasks
请注意,这将导致所有待处理程序在那个时间点运行,而不仅仅是特定的一个。

哦!我明白了 https://docs.ansible.com/ansible/latest/modules/meta_module.html - zx1986
官方文档链接:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/meta_module.html,该文档介绍了meta模块。 - Lamine BA

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接