【问题标题】:Ansible won't see a handler when using the group_by使用 group_by 时,Ansible 不会看到处理程序
【发布时间】:2016-12-21 11:27:33
【问题描述】:

我曾经有一个简单的剧本 (something like this),我在我的所有机器(基于 RH 和 Debian)上运行以更新它们,并为每台更新的机器运行一个脚本(通知处理程序)。

最近我尝试测试一个名为group_by 的新模块,因此在ansible_distribution == "CentOS" 时使用when 条件运行yum update,我将首先收集事实并根据ansible_pkg_mgr 对主机进行分组作为密钥,然后我希望在密钥为 PackageManager_yum 的所有主机上运行 yum update ,请参阅剧本示例:

---
- 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

提前致谢。

【问题讨论】:

    标签: ansible ansible-2.x ansible-handlers


    【解决方案1】:

    您只在其中一场比赛中定义了处理程序。看缩进就很清楚了。

    您为PackageManager_apt 执行的剧本根本没有handlers(它无法访问在单独的剧本中定义的updateX 处理程序),因此 Ansible 抱怨。

    如果您不想复制代码,可以将处理程序保存到单独的文件(我们将其命名为 handlers.yml)并包含在两个游戏中:

      handlers:
        - name: Include common handlers
          include: handlers.yml
    

    注意:Handlers: Running Operations On Change 部分中有一条关于包含处理程序的注释:

    您不能通知在包含内定义的处理程序。从 Ansible 2.1 开始,这确实有效,但是包含必须是静态的。


    最后,您应该考虑将您的剧本转换为角色。

    实现您想要的一种常见方法是使用名称中包含架构的文件名包含任务(在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 中定义处理程序。

    【讨论】:

    • 感谢您的提示。
    猜你喜欢
    • 2017-09-02
    • 2013-03-08
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多