linux - ansible 仅在主机重新启动时才执行其他操作

标签 linux ansible

我有一个剧本,可以根据需要在主机上运行。我遇到了问题,但我自己找不到答案。

我们的主机正在运行我们正在构建的应用程序,并且有一些服务器的更新不会自动完成。目标是在需要时通过 ansible 进行这些更新。问题是:如果在更新过程中主机重新启动,则需要执行其他步骤。但是,如果主机没有重新启动,则需要省略这些步骤。

yml 文件大致如下所示:

---
- name: run updates on demand
  hosts: static-hosts
  roles:
    - update_system
    - update_component1
    - update_component2

这里我需要一些检查。如果上述步骤导致主机重新启动,请执行以下操作。如果它没有重新启动,只需在此处停止。

    - rebuild_component1
    - rebuild_component2

有什么建议吗?我可能会把这一切都弄错了,如果是这样的话,如果我能指出正确的方向,我将不胜感激。谢谢!


想到了可能的解决方案,但尚未经过测试。由于重新启动是可靠的(我的原始 yml 文件中未列出),因此可行的一种方法是使用以下 yml:

---
- name: run updates on demand
  hosts: static-hosts
  roles:
    - update_system
    - update_component1
    - update_component2
  register: updated

- name: reboot
  shell: shutdown -r now
  when: updated.changed

- name: wait for SSH
  (usual "wait for ssh block")

- name: rebuild if rebooted
  roles:
    - rebuild_component1
    - rebuild_component2
  when: updated.changed

我不确定的是 updated.changed 是否真的只在更新需要时触发重启(并因此触发重建)。

最佳答案

我上面列出的“可能的解决方案”有效。

关于linux - ansible 仅在主机重新启动时才执行其他操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36451482/

相关文章:

python - 将参数从 shell 脚本传递到 Python 脚本,而无需在命令行上指定参数

linux - 通过 bash 脚本查找和 Xargs 复制和重命名

ansible - 如何重用我编写的 ansible 角色?

ansible - 将 with_items 应用于多个任务

ruby-on-rails - 尝试安装 RoR 时安装 RVM 失败

linux - yocto 图像版本控制的最佳方法

c - 在 C 宏中执行算术运算

ansible - 当环境变量不存在时,如何分配默认的 Ansible 值?

json - Ansible:如何在 JSON 中打印数字而不是字符串 - 模块 uri

Ansible:删除程序并在命令提示符下输入 'yes'?