Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 418
krm Avatar asked Jul 14 '14 08:07

krm


People also ask

How do you use handlers in Ansible roles?

In a nutshell, handlers are special tasks that only get executed when triggered via the notify directive. Handlers are executed at the end of the play, once all tasks are finished. In Ansible, handlers are typically used to start, reload, restart, and stop services.

How do I run roles in Ansible?

There is no way to directly execute a role. Roles have no explicit setting for which host the role will apply to. Top-level playbooks are the bridge holding the hosts from your inventory file to roles that should be applied to those hosts.

What is difference between tasks and handlers in Ansible?

Handlers are just like regular tasks in an Ansible playbook (see Tasks) but are only run if the Task contains a notify keyword and also indicates that it changed something. For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler.

Can an Ansible handler notify another handler?

Notifying the same handler multiple times will result in executing the handler only once regardless of how many tasks notify it. For example, if multiple tasks update a configuration file and notify a handler to restart Apache, Ansible only bounces Apache once to avoid unnecessary restarts.


1 Answers

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.

like image 181
krm Avatar answered Oct 05 '22 19:10

krm