How to force handler to run before executing a task in Ansible?

ぐ巨炮叔叔 提交于 2019-12-18 11:05:43

问题


I have a playbook which should configure on specified IP, and than connect to this app to configure stuff inside.

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).

My current 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

I need to force the handler to run if configure_app changed before executing 'Change data in app'.


回答1:


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

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.



来源:https://stackoverflow.com/questions/34018862/how-to-force-handler-to-run-before-executing-a-task-in-ansible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!