Ansible 处理程序仅在更改时运行:true
Ansible handler runs only if changed: true
使用 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 会安装并启动它。
如果服务已经安装,但不是 运行,它不会通知处理程序:
任务状态是 changed: false
这意味着,如果它已经出现在 OS.
中,我将无法启动它
是否有任何好的做法可以帮助确保服务已安装并处于 运行 状态?
PS:我可以这样做:
---
# roles/common/tasks/ntp.yml
- name: ntp | installing
yum: name=ntp state=latest
notify: start ntp
changed: true
但我不确定这是个好习惯。
那你为什么不直接添加一个服务任务呢?处理程序通常用于在配置更改后重新启动服务。无论如何,要确保服务 运行,只需添加这样的任务:
- name: Ensure ntp is running
service:
name: ntpd
state: started
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.
仅处理程序 运行 设计更改。如果您更改配置,您通常需要重新启动服务,但如果没有任何更改,则不需要。
你想要的是启动一个服务,如果它还没有 运行ning。为此,您应该使用@udondan 所述的常规任务:
- name: ntp | installing
yum:
name: ntp
state: latest
- name: ntp | starting
service:
name: ntpd
state: started
enabled: yes
Ansible 在设计上是幂等的,因此如果 ntp 尚未 运行ning,则第二个任务只会 运行。 enabled
行将服务设置为在启动时启动。如果这不是所需的行为,请删除此行。
使用 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 会安装并启动它。
如果服务已经安装,但不是 运行,它不会通知处理程序:
任务状态是 changed: false
这意味着,如果它已经出现在 OS.
是否有任何好的做法可以帮助确保服务已安装并处于 运行 状态?
PS:我可以这样做:
---
# roles/common/tasks/ntp.yml
- name: ntp | installing
yum: name=ntp state=latest
notify: start ntp
changed: true
但我不确定这是个好习惯。
那你为什么不直接添加一个服务任务呢?处理程序通常用于在配置更改后重新启动服务。无论如何,要确保服务 运行,只需添加这样的任务:
- name: Ensure ntp is running
service:
name: ntpd
state: started
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.
仅处理程序 运行 设计更改。如果您更改配置,您通常需要重新启动服务,但如果没有任何更改,则不需要。
你想要的是启动一个服务,如果它还没有 运行ning。为此,您应该使用@udondan 所述的常规任务:
- name: ntp | installing
yum:
name: ntp
state: latest
- name: ntp | starting
service:
name: ntpd
state: started
enabled: yes
Ansible 在设计上是幂等的,因此如果 ntp 尚未 运行ning,则第二个任务只会 运行。 enabled
行将服务设置为在启动时启动。如果这不是所需的行为,请删除此行。