总是触发Ansible中的处理程序执行

我使用Ansible来configuration我的开发服务器。

我希望它总是为我开始一些服务。 我有这样的处理程序,但触发处理程序执行没有条件的最好方法是什么,例如使其始终工作?

像这样的东西:

tasks: - name: Trigger handler run_handler: name=nginx-restart 

如果你绝对需要触发一个处理程序,那么这里有两个select:

1)运行一个noop shell命令,它总是会报告为已更改

 - name: trigger nginx-restart command: /bin/true notify: nginx-restart 

2)使用debugging与changed_when:触发一个处理程序

 - debug: msg="trigger nginx-restart" notify: nginx-restart changed_when: true 

还要注意选项1和检查模式:如果使用Ansible版本2.2或更高版本,或者always_run: yes则可能需要使用check_mode: no如果使用早期版本,则在检查模式下不会跳过该任务。 从我的手动testing看来,处理程序仍处于检查模式,但请注意,因为您的情况可能有所不同。

Ansible提供了几种强制处理程序的选项:

1)要始终强制所有处理程序,请运行安全ansible-playbook playbook.yml --force-handlers ,如以下文档所述: https : //github.com/ansible/ansible/issues/4777

2)要强制在手册中的特定位置通知处理程序,可以使用元任务https://docs.ansible.com/playbooks_intro.html

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

3)然而,听起来你只是想确保服务正在运行或重新启动,无论其他任务的结果如何。 在这种情况下,请不要使用处理程序,而要使用调用Ansible service模块的新任务: http ://docs.ansible.com/service_module.html

 tasks: - name: ensure ntp is running service: name=ntp state=started enabled=yes - name: always reload nginx service: name=nginx state=reloaded - name: always restart MySQL service: name=mysql state=restarted 

重新启动服务是一回事。 确保它正在运行是另一回事。 如果你想要确保nginx正在运行,你可以这样做:

 tasks: - name: Ensure nginx is running service: name=nginx state=started