# handlers inside loops

**URL:** <https://forum.ansible.com/t/handlers-inside-loops/17610>\
**Category:** Ansible Project\
**Created:** [February 24, 2015, 7:04pm UTC](https://forum.ansible.com/t/handlers-inside-loops/17610 "2015-02-24T19:04:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacob\_Weber1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jacob\_Weber1](https://forum.ansible.com/u/Jacob_Weber1)\
**Post date:** [February 24, 2015, 7:04pm UTC](https://forum.ansible.com/t/handlers-inside-loops/17610/1 "2015-02-24T19:04:36Z")

</div>

I figured out the solution to this while writing it, but I thought I’d share anyway, in case anyone else finds it useful.

I have a task to update the config files for multiple web servers, which triggers a handler to restart them all.

# tasks/main.yml

- name: update config file  
file: src=httpd-{{ item }}.conf dest=/etc/httpd-{{ item }}.conf  
notify: restart web servers  
with\_items: web\_servers

# handlers/main.yml

- name: restart web servers  
service: httpd-{{ item }} state=restarted  
with\_items: web\_servers

This works, and restarts all web servers. But what if I want to only restart a web server when its config file changes?

The {{ item }} variable doesn’t get passed to the handler, so I can’t use it there. And even if it did, the handler only gets called once.

The solution is to register a variable with the results of the update. Then add a separate handler to only restart the changed servers:

# tasks/main.yml

- name: update config file  
file: src=httpd-{{ item }}.conf dest=/etc/httpd-{{ item }}.conf  
register: configs\_changed  
notify: restart web servers with changed configs  
with\_items: web\_servers

# handlers/main.yml

- name: restart web servers with changed configs  
service: httpd-{{ item.item }} state=restarted  
when: item.changed  
with\_items: configs\_changed.results

The only problem is that if there are multiple tasks that might trigger a restart, you have to register a separate variable and create a separate handler for each one. So the server will get restarted multiple times. Not sure if there’s a better way, but I can live with this limitation.

Jacob

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [February 25, 2015, 2:06pm UTC](https://forum.ansible.com/t/handlers-inside-loops/17610/2 "2015-02-25T14:06:49Z")

</div>

the normal pattern is to have this taken care of by a hosts loop

- hosts: webservers  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;...

but your case seems to imply having multiple webservers per host and  
your use of registered vars and |changed seems to be the adequate  
solution. To expand on that when you have more of these variables you  
can do:

with\_items:  
&nbsp;&nbsp;&nbsp;- web\_servers.results  
&nbsp;&nbsp;&nbsp;- app\_servers.results  
&nbsp;&nbsp;&nbsp;- etc ...

which will be aggregated into 'item'.
