gpt4 book ai didi

Ansible 处理程序仅在更改时运行 : true

转载 作者:行者123 更新时间:2023-12-02 10:46:36 25 4
gpt4 key购买 nike

使用 Ansible 安装 ntp,我通知处理程序以启动 ntpd 服务:
任务:

---
# roles/common/tasks/ntp.yml
- name: ntp | installing
yum: name=ntp state=latest
notify: start ntp

处理程序:

---
# roles/common/handlers/main.yml

- name: start ntp
service: name=ntpd state=started

如果服务尚未安装,ansible 会安装并启动它。
如果服务已安装,但未运行,则不会通知处理程序:
任务状态已更改:false
这意味着,如果它已经存在于操作系统中,我无法启动它。

是否有任何好的做法可以帮助确保服务已安装并处于运行状态?

PS:我可以这样做:

---
# roles/common/tasks/ntp.yml
- name: ntp | installing
yum: name=ntp state=latest
notify: start ntp
changed: true

但我不确定这是一个好的做法。

最佳答案

来自Intro to Playbooks guide :

As we’ve mentioned, modules are written to be ‘idempotent’ and can relay when they have made a change on the remote system. Playbooks recognize this and have a basic event system that can be used to respond to change.

These ‘notify’ actions are triggered at the end of each block of tasks in a playbook, and will only be triggered once even if notified by multiple different tasks.

处理程序仅在设计发生变化时运行。如果更改配置,您通常需要重新启动服务,但如果没有任何更改,则不想这样做。

您想要的是启动一个服务(如果它尚未运行)。为此,您应该使用 @udondan 所描述的常规任务:

- name: ntp | installing
yum:
name: ntp
state: latest

- name: ntp | starting
service:
name: ntpd
state: started
enabled: yes

Ansible 在设计上是幂等的,因此第二个任务仅在 ntp 尚未运行时才会运行。 enabled 行会将服务设置为在启动时启动。如果这不是所需的行为,请删除此行。

关于Ansible 处理程序仅在更改时运行 : true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35600125/

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