仅供参考,我正在以“拉模式”而不是推模式运行我的Ansible剧本。因此,我的节点将通过Hipchat发布他们的任务结果。
说到这里,我有一个安装RPM的任务。安装成功后,节点通过Hipchat通知任务已成功运行。现在,如果任务失败,我强制它通知带有“--force-handlers”参数的hipchat。我的问题是,有没有办法根据任务的结果调用特定的处理程序?
- 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 :(得分:1)
在这种情况下,我使用多个处理程序。请参阅以下示例:
file 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