krm
krm

Reputation: 853

Ansible Roles and handlers - Cannot get role handlers to work

I need to set up Apache/mod_wsgi in Centos 6.5 so my main YAML file is as such:

---
- hosts: dev
  tasks:
    - name: Updates yum installed packages
      yum: name=* state=latest


- hosts: dev
  roles:
    - { role: apache }

This should update all yum-installed packages then execute the apache role.

The apache role is configured to install Apache/mod_wsgi, set Apache to start at boot time and restart it. The following are the contents of roles/apache/tasks/main.yml:

---
- name: Installs httpd and mod_wsgi
  yum: name={{ item }} state=latest
  with_items:
    - httpd
    - mod_wsgi
  notify:
    - enable httpd
    - restart httpd

And the handlers in roles/apache/handlers/main.yml:

---
- name: enable httpd
  service: name=httpd enabled=yes

- name: restart httpd
  service: name=httpd state=restarted

The handlers do not seem to run since the following output is given when I execute the playbook:

PLAY [dev] ******************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [dev.example.com]

TASK: [Updates yum installed packages] **************************************** 
ok: [dev.example.com]

PLAY [dev] ******************************************************************** 

GATHERING FACTS *************************************************************** 
ok: [dev.example.com]

TASK: [apache | Installs httpd and mod_wsgi] ********************************** 
ok: [dev.example.com] => (item=httpd,mod_wsgi)

PLAY RECAP ******************************************************************** 
dev.example.com            : ok=4    changed=0    unreachable=0    failed=0 

And when I vagrant ssh into the virtual machine, sudo service httpd status shows httpd is stopped and sudo chkconfig --list shows it has not been enabled to be started by init.

I'm just starting out with Ansible, so is there something obvious I could be missing?

Upvotes: 24

Views: 30395

Answers (1)

krm
krm

Reputation: 853

Well, to answer my own question, I realized that there's a subtle point I missed:

http://docs.ansible.com/playbooks_intro.html#handlers-running-operations-on-change

Specifically, the notify signal is produced only if the task introduces a change. So for my use case I think I'll go with enabling and starting Apache in standalone tasks instead of relying on change signal handlers.

Upvotes: 25

Related Questions