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

How to notify a remote service using Ansible?

· 52CE 我爱测

In order to let checks appear in Uchiwa when a new sensu-client is added, the sensu-server and sensu-api need to be restarted. At the moment there are 5 clients and one server. Everytime a new sensu-client is added using ansible …

问题原文

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

In order to let checks appear in Uchiwa when a new sensu-client is added, the sensu-server and sensu-api need to be restarted. At the moment there are 5 clients and one server. Everytime a new sensu-client is added using ansible the checks do not appear in Uchiwa. At the moment, I need to login to the sensu-server and restart the services. How to automate this using ansible?

According to this documentation there are handlers:

handlers:
    - name: restart memcached
      service: name=memcached state=restarted
      listen: "restart web services"
    - name: restart apache
      service: name=apache state=restarted
      listen: "restart web services" 

that could be called using notify:

tasks:
    - name: restart everything
      command: echo "this task will restart the web services"
      notify: "restart web services"

so that the service will be restarted if a change occurs in for example the config, but how to notify a remote service, e.g. notify service on IP-B from IP-A?

已采纳解答

原帖链接 · Baptiste Mille-Mathias · CC BY-SA 3.0

(listen is a 2.2 new feature which is not released as of writing, so I won't use it)

A notify is just like a regular tasks (or so) except it is triggered by an event, so basically you could do something like that, this is kind of ugly (and insecure as the machine should have ssh access to the remote servers) but should work.

handlers:
    - name: restart my remote service
      command: ssh user@myserver -- service restart myservice

But Ansible has a more elegant manner to handle this with delegate_to

handlers:
    - name: restart my remote service
      service: name=myserver state=restarted
      delegate_to: myserver

What it does, it the handler task will be executed on the server(s) you've have delegate the task. and then plug-in to your task as you told in the question.

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

资料来源与适用范围

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

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

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