大网数据广告活动
服务器运维问答

Is it possible to notify a group of services instead of individual in Ansible?

· 52CE 我爱测

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

问题原文

原帖链接 · 030 · CC BY-SA 4.0

- 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

已采纳解答

原帖链接 · Henrik Pingel · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-08-10)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。