1

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

所以假设我这样称呼我的模块:

- 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模块(实际上它是一个动作插件)只会在第一个被处理的主机上暂停。因此,如果第一台主机失败,它将暂停,如果第一台主机通过并且所有其他主机都失败,它将简单地继续所有主机。

这似乎是预期的行为。从文档

pause 模块无需任何特殊考虑即可集成到异步/并行化剧本中(另请参阅:滚动更新)。当使用带有serialplaybook 参数的暂停时(如在滚动更新中),您只会被提示一次当前主机组。

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

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

任何人都可以看到解决方案如何:

  • 为每个主机暂停(失败的)
  • 暂停一次,以防任何主机出现故障
4

2 回答 2

3

这个错误报告给了我使用循环的灵感。以下解决方案要求分别确认每个失败的主机:

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

- name: Saving downtime state
  set_fact:
    downtime_failed: "{{ downtime | failed }}"

- name: Request user confirmation to proceed in case downtime could not be set
  pause:
    prompt: 'Downtime could not be set for {{ item }}. Do you want to proceed? Press return to continue. Press Ctrl+c and then "a" to abort'
  when: "{{ hostvars[item]['downtime_failed'] }}"
  with_items: "{{ play_hosts }}"

由于该pause模块仅针对清单中列出的第一个主机运行,因此我们遍历所有可用主机 ( play_hosts)。要从所有其他主机访问状态,我们首先需要将结果存储为事实 ( set_fact),然后我们可以通过 访问它hostvars,其中包含当前播放的所有主机的所有事实。

于 2016-02-03T05:54:29.623 回答
3

为了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模块在组的第一台主机上运行。通过这两个任务,我们获得了每个主机的输入,并为所有主机设置了一个可用的新事实。

于 2017-06-21T11:39:26.213 回答