ansible - 无论有多少目标,如何只运行一次 ansible 任务

标签 ansible hipchat

考虑以下 Ansible 任务:

- name: stop tomcat
  gather_facts: false
  hosts: pod1
  pre_tasks:
  - include_vars:
      dir: "vars/{{ environment }}"
  vars:
    hipchat_message: "stop tomcat pod1 done."
    hipchat_notify: "yes"
  tasks:
    - include: tasks/stopTomcat8AndClearCache.yml
    - include: tasks/stopHttpd.yml
    - include: tasks/hipchatNotification.yml

这会停止 n 个服务器上的 tomcat。我想让它做的是在完成此操作后发送一个 hipchat 通知。但是,此代码会为任务发生的每个服务器发送单独的 hipchat 消息。这会使 hipchat 窗口充满冗余消息。在所有目标上完成停止 tomcat/停止 httpd 任务后,有没有办法让 hipchat 任务发生一次?我希望任务关闭所有服务器上的 tomcat,然后发送一条聊天消息“tomcat 在 pod 1 上停止”。

最佳答案

您可以有条件地仅在其中一台 pod1 主机上运行 hipchat 通知任务。

- include: tasks/hipChatNotification.yml
  when: inventory_hostname == groups.pod1[0]

或者,如果您不需要之前游戏中的任何变量,则只能在本地主机上运行它。

- name: Run notification
  gather_facts: false
  hosts: localhost
  tasks:
  - include: tasks/hipchatNotification.yml

您还可以使用 run_once标记任务本身。

- name: Do a thing on the first host in a group.
  debug: 
    msg: "Yay only prints once"
  run_once: true

- name: Run this block only once per host group
  block:
  - name: Do a thing on the first host in a group.
    debug: 
      msg: "Yay only prints once"
  run_once: true

关于ansible - 无论有多少目标,如何只运行一次 ansible 任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55481560/

相关文章:

Ansible:运行失败的任务

ssh - **“失败”:true} msg:无法锁定用于独占操作的FATAL:所有主机都已失败—正在中止**

HipChat API token v1 与 v2

javascript - HipChat 机器人 JS 正则表达式

ruby - 如何通过 API 从 Hipchat 获取房间的所有消息历史记录?

python - 如何从 ansible-playbook 获取更多可用的错误消息?

ansible - 在 Ansible 上设置环境变量

ansible - 回调插件不适用于 Ansible v2.0

javascript - 让 Hubot 在 HipChat 中发送直接消息