我正在尝试在同一角色的处理程序上运行通知,并且该角色被包含在依赖项中。
这是我的剧本
root@monitor:/etc/ansible# cat monitor.yml
---
- hosts: local
connection: local
become: yes
become_user: root
roles:
- common
- role: sensu
sensu_install_server: true
我的常用文件如下
root@monitor:/etc/ansible# cat roles/common/tasks/main.yml
- name: Add the OS specific variables
include_vars: '{{ ansible_os_family }}.yml'
#
# ansible run script
- name: copy the ansible-run script
copy:
src=ansible-run.py
dest=/usr/bin/ansible-run
owner=root
group=root
mode=0700
- cron: name="cron ansible-run" minute="*/5" job="/usr/bin/ansible-run > /dev/null 2&1"
#
# ntp
- name: Install ntp
package: name=ntp state=present
- name: Start/stop ntp service
service: name={{ ntp_service_name }} state=started enabled=yes
root@monitor:/etc/ansible# cat roles/common/meta/main.yml
---
dependencies:
- { role: users }
- { role: sensu }
我的 sensu 角色是我希望通知在tasks/client.yml
root@monitor:/etc/ansible# cat roles/sensu/tasks/main.yml
- include: common.yml
- include: server.yml
when: sensu_install_server
ignore_errors: true
- include: client.yml
root@monitor:/etc/ansible# cat roles/sensu/tasks/client.yml
- name: reload ansible_local
setup: filter=ansible_local
- name: copy plugins files
copy:
src=files/sensu/plugins/
dest=/etc/sensu/plugins/
owner=sensu
group=sensu
mode=0750
notify:
- restart sensu client
- name: generate config files
template:
src=client.json.j2
dest=/etc/sensu/conf.d/client.json
owner=sensu
group=sensu
mode=0640
backup=yes
notify:
- restart sensu client
- name: enable sensu-client to survive reboot
service:
name=sensu-client
enabled=yes
state=started
root@monitor:/etc/ansible# cat roles/sensu/handlers/main.yml
---
# handlers file for sensu
- name: restart sensu server
service: name=sensu-{{ item }} state=restarted
with_items:
- server
- api
- name: restart sensu client
service: name=sensu-client state=restarted
- name: restart uchiwa service
service: name=uchiwa state=restarted
- name: restart nginx service
service: name=nginx state=restarted
从运行来看,client.json 已经发生改变,但我从未看到调用处理程序的通知。
答案1
首先,由于文件类型错误,我猜你的处理程序会被忽略:
角色/sensu/处理程序/主页面
应该:roles/sensu/handlers/main.yml
其次,请注意,尽管您有几个处理程序,但唯一被通知程序通知的处理程序是:restart sensu client
。因此其他处理程序将不会运行。
处理程序是任务列表,与常规任务并无太大区别,它们由全局唯一名称引用。处理程序是通知程序所通知的对象。如果没有通知处理程序,处理程序将不会运行。无论有多少通知处理程序,处理程序都只会运行一次,即在特定剧集中的所有任务完成后。