gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:38:50 25 4
gpt4 key购买 nike

考虑以下 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com