使用 group_by 时 Ansible 不会看到处理程序
Ansible won't see a handler when using the group_by
我曾经有一个简单的剧本 (something like this),我 运行 在我所有的机器(基于 RH 和 Debian)上更新它们,并且对于每台更新的机器 运行 一个脚本(通知处理程序)。
最近我试图测试一个名为 group_by
的新模块,所以当 ansible_distribution == "CentOS"
时,我将使用 when
条件来 运行 yum update
首先收集事实并根据 ansible_pkg_mgr
作为键对主机进行分组,然后我正在寻找 运行 yum update 在所有键为 PackageManager_yum 的主机上,请参阅剧本示例:
---
- hosts: all
gather_facts: false
remote_user: root
tasks:
- name: Gathering facts
setup:
- name: Create a group of all hosts by operating system
group_by: key=PackageManager_{{ansible_pkg_mgr}}
- hosts: PackageManager_apt
gather_facts: false
tasks:
- name: Update DEB Family
apt:
upgrade=dist
autoremove=yes
install_recommends=no
update_cache=yes
when: ansible_os_family == "Debian"
register: update_status
notify: updateX
tags:
- deb
- apt_update
- update
- hosts: PackageManager_yum
gather_facts: false
tasks:
- name: Update RPM Family
yum: name=* state=latest
when: ansible_os_family == "RedHat"
register: update_status
notify: updateX
tags:
- rpm
- yum
- yum_update
handlers:
- name: updateX
command: /usr/local/bin/update
这是我收到的错误消息,
PLAY [all] ********************************************************************
TASK [Gathering facts] *********************************************************
Wednesday 21 December 2016 11:26:17 +0200 (0:00:00.031) 0:00:00.031 ****
....
TASK [Create a group of all hosts by operating system] *************************
Wednesday 21 December 2016 11:26:26 +0200 (0:00:01.443) 0:00:09.242 ****
TASK [Update DEB Family] *******************************************************
Wednesday 21 December 2016 11:26:26 +0200 (0:00:00.211) 0:00:09.454 ****
ERROR! The requested handler 'updateX' was not found in either the main handlers list nor in the listening handlers list
提前致谢。
您只在一个剧本中定义了处理程序。看缩进就很清楚了
您为 PackageManager_apt
执行的播放根本没有 handlers
(它无法访问单独播放中定义的 updateX
处理程序),因此 Ansible 抱怨.
如果您不想重复代码,您可以将处理程序保存到一个单独的文件(我们将其命名为 handlers.yml
)并包含在两个播放中:
handlers:
- name: Include common handlers
include: handlers.yml
注意:Handlers: Running Operations On Change 部分中有关于包含处理程序的注释:
You cannot notify a handler that is defined inside of an include. As of Ansible 2.1, this does work, however the include must be static.
最后,您应该考虑将剧本转换为角色。
实现您想要的目标的一种常用方法是使用名称中包含体系结构的文件名来包含任务(在 tasks/main.yml
中):
- include: "{{ architecture_specific_tasks_file }}"
with_first_found:
- "tasks-for-{{ ansible_distribution }}.yml"
- "tasks-for-{{ ansible_os_family }}.yml"
loop_control:
loop_var: architecture_specific_tasks_file
然后在 handlers/main.yml
中定义处理程序。
我曾经有一个简单的剧本 (something like this),我 运行 在我所有的机器(基于 RH 和 Debian)上更新它们,并且对于每台更新的机器 运行 一个脚本(通知处理程序)。
最近我试图测试一个名为 group_by
的新模块,所以当 ansible_distribution == "CentOS"
时,我将使用 when
条件来 运行 yum update
首先收集事实并根据 ansible_pkg_mgr
作为键对主机进行分组,然后我正在寻找 运行 yum update 在所有键为 PackageManager_yum 的主机上,请参阅剧本示例:
---
- hosts: all
gather_facts: false
remote_user: root
tasks:
- name: Gathering facts
setup:
- name: Create a group of all hosts by operating system
group_by: key=PackageManager_{{ansible_pkg_mgr}}
- hosts: PackageManager_apt
gather_facts: false
tasks:
- name: Update DEB Family
apt:
upgrade=dist
autoremove=yes
install_recommends=no
update_cache=yes
when: ansible_os_family == "Debian"
register: update_status
notify: updateX
tags:
- deb
- apt_update
- update
- hosts: PackageManager_yum
gather_facts: false
tasks:
- name: Update RPM Family
yum: name=* state=latest
when: ansible_os_family == "RedHat"
register: update_status
notify: updateX
tags:
- rpm
- yum
- yum_update
handlers:
- name: updateX
command: /usr/local/bin/update
这是我收到的错误消息,
PLAY [all] ********************************************************************
TASK [Gathering facts] *********************************************************
Wednesday 21 December 2016 11:26:17 +0200 (0:00:00.031) 0:00:00.031 ****
....
TASK [Create a group of all hosts by operating system] *************************
Wednesday 21 December 2016 11:26:26 +0200 (0:00:01.443) 0:00:09.242 ****
TASK [Update DEB Family] *******************************************************
Wednesday 21 December 2016 11:26:26 +0200 (0:00:00.211) 0:00:09.454 ****
ERROR! The requested handler 'updateX' was not found in either the main handlers list nor in the listening handlers list
提前致谢。
您只在一个剧本中定义了处理程序。看缩进就很清楚了
您为 PackageManager_apt
执行的播放根本没有 handlers
(它无法访问单独播放中定义的 updateX
处理程序),因此 Ansible 抱怨.
如果您不想重复代码,您可以将处理程序保存到一个单独的文件(我们将其命名为 handlers.yml
)并包含在两个播放中:
handlers:
- name: Include common handlers
include: handlers.yml
注意:Handlers: Running Operations On Change 部分中有关于包含处理程序的注释:
You cannot notify a handler that is defined inside of an include. As of Ansible 2.1, this does work, however the include must be static.
最后,您应该考虑将剧本转换为角色。
实现您想要的目标的一种常用方法是使用名称中包含体系结构的文件名来包含任务(在 tasks/main.yml
中):
- include: "{{ architecture_specific_tasks_file }}"
with_first_found:
- "tasks-for-{{ ansible_distribution }}.yml"
- "tasks-for-{{ ansible_os_family }}.yml"
loop_control:
loop_var: architecture_specific_tasks_file
然后在 handlers/main.yml
中定义处理程序。