在Ansible中,要在多个任务块之间共享通知处理程序,可以使用notify和handler模块。
通知处理程序通常会在任务成功完成后执行。当某个任务块中的任意任务发生变化时,可以使用notify将其通知给相应的处理程序。处理程序只有在所有任务块都已执行成功时才会被执行。
下面是一个示例,其中有两个任务块(task_block_one和task_block_two),并且它们共享相同的处理程序(restart_service):
name: playbook with two task blocks hosts: all become: true
tasks:
name: task block one block:
name: install package one apt: name: package_one state: present
name: configure package one template: src: package_one.conf.j2 dest: /etc/package_one.conf notify: restart_service
name: task block two block:
name: install package two apt: name: package_two state: present
name: configure package two template: src: package_two.conf.j2 dest: /etc/package_two.conf notify: restart_service
handlers:
在这个示例中,当任务块one或任务块two中的任意任务发生变化时,都会发送一个通知到处理程序restart_service。处理程序在所有任务块都成功执行后才会被执行。