我有一个剧本,可以根据需要在主机上运行。我遇到了问题,但我自己找不到答案。
我们的主机正在运行我们正在构建的应用程序,并且有一些服务器的更新不会自动完成。目标是在需要时通过 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/