ansible - 每个主机的 "pause"

标签 ansible ansible-2.x

在滚动更新之前,我想在我们的监控工具中为每台主机设置停机时间。我为此创建了一个自定义模块。设置停机时间时可能会出现问题,我们无法解决。在那种情况下,我想让用户选择是否应中止部署或继续部署而不设置停机时间。

假设我这样调用我的模块:

- downtime:
    duration: 5m
    comment: whatever
  ignore_errors: true
  register: downtime

所以我忽略了能够继续的错误。否则设置停机时间失败的主机将不会被进一步处理。

在下一步中,我希望用户手动确认他是否要为每个没有设置停机时间的主机继续操作。

- name: Request user confirmation to proceed in case downtime could not be set
  pause:
    prompt: 'Downtime could not be set for all hosts. Do you want to proceed? Press return to continue. Press Ctrl+c and then "a" to abort'
  when: "{{ downtime | failed }}"

不幸的是,pause 模块(实际上它是一个 Action 插件)只会为第一个被处理的主机暂停。因此,如果第一台主机失败,它将暂停,如果第一台主机通过而所有其他主机失败,它将继续处理所有主机。

这似乎是预期的行为。来自docs :

The pause module integrates into async/parallelized playbooks without any special considerations (see also: Rolling Updates). When using pauses with the serial playbook parameter (as in rolling updates) you are only prompted once for the current group of hosts.

所以无论如何,即使我使用 serial: 1(在这种情况下是不可能的)暂停也只会在第一个主机上停止。

现在我只是无条件地暂停,让用户决定是否要继续,无论停机任务是否失败。但由于失败应该非常罕见,这是我想避免的手动步骤。

谁能看到解决方案:

  • 每个 主机(失败的)暂停
  • 暂停一次,以防任何主机失败

最佳答案

为了在一组主机上运行 pause 模块,我做了这个技巧:

- pause:
    prompt: "{{ item }} will be restarted. Enter 'YES' to restart"
  register: input
  with_items: "{{ play_hosts }}"

- set_fact:
    user_input: "{{ item.user_input }}"
  with_items: "{{ hostvars[play_hosts.0].input.results }}"
  when: item.item == ansible_hostname|upper

正如udondan所说,pause模块在组的第一台主机上运行。通过这两项任务,我们获得每个主机的输入并为所有主机设置一个可用的新事实。

关于ansible - 每个主机的 "pause",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35169149/

相关文章:

ansible - ansible中的json_query查找键值并打印json对象中的另一个键

windows - 如何从Powershell到Ansible获取负返回码

amazon-web-services - 从ansible中注册的变量中检索键的值

ansible - 使用列表迭代哈希

ansible - 运行 ansible playbook 时缺少主机信息

amazon-web-services - 需要从AWS中已安装应用程序的机器拍摄Docker镜像或容器的镜像

postgresql - 无法使用 Ansible postgresql_user 模块创建用户

azure - 是否可以通过azure中的ansible覆盖或创建新版本的 secret ?

ssl - kolla-ansible TLS 部署失败

java - apt_repository 模块失败