2015-06-23 16 views
2

仅供参考,我在“pull-mode”而非push模式下运行我的Ansible游戏手册。因此,我的节点将通过Hipchat发布他们的任务结果。Ansible有条件的处理程序的任务?

就这样说,我有一个安装RPM的任务。安装成功后,节点通过Hipchat通知该任务已成功运行。现在,如果任务失败,我强迫它通知hipchat /“ - force-handlers”参数。我的问题是,有没有办法根据任务的结果调用特定的处理程序?

任务

- name: Install Perl modules 
    command: sudo rpm -Uvh {{ rpm_repository }}/{{ item.key }}-{{ item.value.svn_tag }}.rpm --force 
    with_dict: deploy_modules_perl 
    notify: announce_hipchat 

处理器

- name: announce_hipchat 
    local_action: hipchat 
     from="deployment" 
     token={{ hipchat_auth_token }} 
     room={{ hipchat_room }} 
     msg="[{{ ansible_hostname }}] Successfully installed RPMs!" 
     validate_certs="no" 
+0

您可以在处理程序中使用'when','with_items'等。条件会给你你想要的吗?从您的解释和示例代码中,我不太确定您要做什么...... –

+0

本部分的文档可能会有所帮助:https://docs.ansible.com/playbooks_error_handling.html#ignoring-failed-commands您可以忽略失败,然后根据失败或成功完成不同的事情。 – David

回答

0

在这种情况下,我使用了多个处理程序。见下面的例子:

文件site.yml

- hosts: all 
    gather_facts: false 
    force_handlers: true 
    vars: 
    - cmds: 
     echo: hello 
     eccho: hello 
    tasks: 
    - name: echo 
    command: "{{ item.key }} {{ item.value }}" 
    register: command_result 
    with_dict: "{{ cmds }}" 
    changed_when: true 
    failed_when: false 
    notify: 
    - "hipchat" 
    handlers: 
    - name: "hipchat" 
    command: "/bin/true" 
    notify: 
    - "hipchat_succeeded" 
    - "hipchat_failed" 
    - name: "hipchat_succeeded" 
    debug: 
     var: "{{ item }}" 
    with_items: "{{ command_result.results | selectattr('rc', 'equalto', 0) | list }}" 
    - name: "hipchat_failed" 
    debug: 
     var: "{{ item }}" 
    with_items: "{{ command_result.results | rejectattr('rc', 'equalto', 0) | list }}" 

使用命令

ansible-playbook -c local -i "localhost," site.yml 

小心:在加入的Jinja2使用测试过滤器'equalsto'2.8

相关问题