ansible-仅当角色中的任何任务发生更改时才运行处理程序 [英] ansible - run handler only if any task in role changed

查看:169
本文介绍了ansible-仅当角色中的任何任务发生更改时才运行处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为reboot server创建了一个处理程序,并且我的角色是使用几种配置来设置操作系统(此角色中大约有6个任务),并且我想仅在整个任务中的任何一项时触发reboot server处理程序角色会发生变化,并且在完成整个角色后也会发生变化.

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.

我试图在剧本中添加"notify"作为角色.但是出现了ERROR! 'notify' is not a valid attribute for a Play

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"之后,如果角色中的任何任务的状态为已更改",则将触发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模块也可以用来代替command: 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-仅当角色中的任何任务发生更改时才运行处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆