- name: be sure check-http.rb is copied
copy:
src: /tmp/sensu-plugins-http/bin/check-http.rb
dest: /etc/sensu/plugins/check-http.rb
remote_src: true
owner: sensu
group: sensu
mode: 0500
notify:
- restart sensu-api
- restart sensu-client
- restart sensu-server
- name: be sure check_websites.json is copied
template:
src: check_websites.j2
dest: "{{ sensu_conf_d }}/check_websites.json"
register: check_websites_config
notify:
- restart sensu-api
- restart sensu-client
- restart sensu-server
无需重复:
notify:
- restart sensu-api
- restart sensu-client
- restart sensu-server
是否可以使其简洁一些,例如:
notify:
- restart sensu-services
预期答案
@knowhy 建议此解决方案,但我正在寻找一种能够在处理程序中而不是在任务目录中组合多次重启的解决方案
答案1
截至目前,Ansible 中还没有这样的功能。
有一些或多或少丑陋的解决方法(如问题中所链接的),我不会在这里重复。
我能想到的最佳解决方案是编写一个处理程序来组合这些任务。假设问题中的处理程序只是重新启动服务,则with_items
可以使用如下循环:
- name: restart sensu-services
service:
name: "{{ item }}"
state: restarted
with_items:
- sensu-api
- sensu-client
- sensu-server
有一个打开功能请求支持块作为处理程序。这对于此用例来说是一个很好的抽象。
答案2
具有 listen 属性。
notify:
- "restart sensu-services"
handlers:
- name: restart sensu api
listen: "restart sensu-services"
service: name=sensu-api state=restarted
ignore_errors: true
- name: restart sensu service
listen: "restart sensu-services"
service: name=sensu-service state=restarted
ignore_errors: true
- name: restart sensu client
listen: "restart sensu-services"
service: name=sensu-client state=restarted
ignore_errors: true