1
- 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

Instead of repeating:

notify:
  - restart sensu-api
  - restart sensu-client
  - restart sensu-server

is it possible to make it concise, e.g.:

notify:
  - restart sensu-services

Expected answer

@knowhy suggested this solution, but I am looking for a solution that is able to combine multiple restarts in a handler and not in the tasks directory

Eddie C.
  • 535
  • 1
  • 3
  • 12
030
  • 5,901
  • 13
  • 68
  • 110

2 Answers2

7

There is with the listen attribute.

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
Eddie C.
  • 535
  • 1
  • 3
  • 12
njefsky
  • 171
  • 1
  • 1
4

There is no such functionality available in Ansible as of today.

There are some more or less ugly workarounds (as linked in the question) which I won't repeat here.

The best solution I can think of would be to write a handler which combines these tasks. Assuming that the handler in the question asked, just restarts services, a with_items loop could be used like this:

- name: restart sensu-services
  service:
    name: "{{ item }}"
    state: restarted
  with_items:
    - sensu-api
    - sensu-client
    - sensu-server

There is an open feature request to support blocks as handlers. That would be a good abstraction for this usecase.

Paul
  • 3,037
  • 6
  • 27
  • 40
Henrik Pingel
  • 9,380
  • 2
  • 28
  • 39