Restart only changed services when using with_items

38 views
Skip to first unread message

Darren Hobbs

unread,
May 29, 2014, 1:58:41 PM5/29/14
to ansible...@googlegroups.com
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

Michael DeHaan

unread,
May 31, 2014, 12:15:55 PM5/31/14
to ansible...@googlegroups.com
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.




--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d21d9016-ab17-4542-b4e8-1ae74246c60a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages