问题描述
我为重启服务器
创建了一个处理程序,并且我有一个角色来设置操作系统的几个配置(这个角色大约有6个任务),我想触发重启服务器
仅在整个角色中的任何任务发生更改时以及在整个角色完成后更改时才处理.
I created a handler to reboot server
and I have a role which sets up the os with several configuration (around 6 tasks in this role) and I want to trigger reboot server
handler only if any of the task in the entire role is changed and that too after completion of entire role.
我试图在角色的剧本中加入通知".但得到错误 ERROR!通知"不是播放的有效属性
I tried to put 'notify' at the playbook for the role. but got error that ERROR! 'notify' is not a valid attribute for a Play
site.yml
---
- name: Setup OS parameters
hosts: master_servers
roles:
- os_prep
tags: os_prep
notify:
- restart server
重新启动服务器的处理程序
handler to reboot server
---
- name: restart server
command: /sbin/shutdown -r now
async: 0
poll: 0
ignore_errors: true
notify:
- check server status
- name: check server status
wait_for:
port: 22
host: '{{ inventory_hostname }}'
search_regex: OpenSSH
delay: 10
timeout: 60
connection: local
运行整个角色'os_prep'后,如果角色中的任何任务处于'changed'状态,则触发restart server
处理程序.
After running the entire role 'os_prep', if any of the task in the role has 'changed' status, then restart server
handler to be triggered.
推荐答案
notify
是任务的属性,而不是游戏的属性.因此,您应该将 notify: restart server
添加到您角色的所有任务中.假设您的所有任务都在 roles/os_prep/tasks/main.yml
中.它看起来像这样:
The notify
is an attribute for a task, not for a play. So you should add notify: restart server
to all your tasks of your role. Let's say all your tasks are in roles/os_prep/tasks/main.yml
. It would look like something like this:
---
- name: Configure this
template:
src: myConfig.cfg.j2
dest: /etc/myConfig.cfg
notify: restart server
- name: Change that
moduleX:
…
notify: restart server
- name: Add users
user:
name: "{{ item.key }}"
home: "/home/{{ item.key }}"
uid: "{{ item.value.uid }}"
with_dict: "{{ users }}"
notify: restart server
- …
处理程序的行为将按照您的预期进行.如果这些任务中的任何一个获得 changed
状态,它将在播放结束时运行重启(仅一次).
The behavior of the handler will proceed like you expect. If any of those tasks get the changed
status, it will run the reboot (only once) at the end of the play.
请注意,根据我的说法,您不应将 notify
应用于不需要重新启动的任务.通常只有少数东西需要重启服务器.在我上面的示例中,添加用户之后不需要重新启动.大多数情况下,重新启动服务就足够了.但当然,我不知道您的用例.
Note that according to me you should not apply the notify
to the task that don't need a reboot. Usually only few stuffs need a server reboot. In my example here above adding user don't need a reboot afterward. And most of the time a service restart will be enough. But of course, I don't know your use-case.
我看到您链接了处理程序.请注意,您也可以使用处理程序的 listen
属性来执行此操作.在您的任务中,您宁愿 notify: Restart and wait server
,您的 roles/os_prep/handlers/main.yml
将如下所示:
I see that you chain your handlers. Be aware that you could also use the listen
attribute of handlers to do so. In you task you rather notify: Restart and wait server
, and your roles/os_prep/handlers/main.yml
will look like this:
---
- name: restart server
command: /sbin/shutdown -r now
async: 0
poll: 0
ignore_errors: true
listen: Restart and wait server
- name: check server status
wait_for:
port: 22
host: '{{ inventory_hostname }}'
search_regex: OpenSSH
delay: 10
timeout: 60
connection: local
listen: Restart and wait server
注意事项 2
请注意,还有一个 reboot
模块可以用来代替 命令:shutdown -r
.
Note 2
Please be aware that there is a reboot
module too that you could use in place of the command: shutdown -r
.
这里是文档:https://docs.ansible.com/ansible/latest/modules/reboot_module.html
这篇关于ansible - 仅当角色中的任何任务发生更改时才运行处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!