# Restart only changed services when using with\_items

**URL:** <https://forum.ansible.com/t/restart-only-changed-services-when-using-with-items/15366>\
**Category:** Ansible Project\
**Created:** [May 29, 2014, 5:58pm UTC](https://forum.ansible.com/t/restart-only-changed-services-when-using-with-items/15366 "2014-05-29T17:58:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darren\_Hobbs](https://avatars.discourse-cdn.com/v4/letter/d/91b2a8/32.png) [@Darren\_Hobbs](https://forum.ansible.com/u/Darren_Hobbs)\
**Post date:** [May 29, 2014, 5:58pm UTC](https://forum.ansible.com/t/restart-only-changed-services-when-using-with-items/15366/1 "2014-05-29T17:58:40Z")

</div>

Hi,

We have a bunch of services, defined as a list of {{ app.name }} and {{ app.version }} variables. We template our upstart files so ansible knows if a new version is being deployed or not (the file only changes if the version number increments). What I’d like to do is only bounce the services whose versions have changed. Here’s what we have at present:

- name: create upstart configs  
template: src=upstart.conf  
dest=/etc/init/{{ item.name }}.conf  
sudo: yes  
with\_items: apps  
register: app\_upstart\_config

- name: stop  
with\_items: apps  
when: app\_upstart\_config.changed  
shell: stop {{ item.name }}  
sudo: yes  
ignore\_errors: yes

- name: start  
with\_items: apps  
when: app\_upstart\_config.changed  
shell: start {{ item.name }}  
sudo: yes

What this seems to be doing at present is bouncing all the apps if any of them change. If none of them change it doesn’t bounce them, so it’s nearly there! I suspect that with\_items is interacting with register in an unanticipated way. Any help would be gratefully appreciated!

Thanks,  
-Darren

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 31, 2014, 4:15pm UTC](https://forum.ansible.com/t/restart-only-changed-services-when-using-with-items/15366/2 "2014-05-31T16:15:50Z")

</div>

Take a look at handlers in Ansible for the most elegant way to fire events on change - what you have here is fairly non-idiomatic.

Also the repeating of “sudo” here can be simplified by moving “sudo” up to the play level.

Also you should be using the service module and declaring state versus running explicit commands.

There was a minor bug on the most recent 1.6.X with handlers that would bounce them all on a single change which is already fixed in the development branch, and we’re cutting another small update after we close a few more larger tickets.
