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
--
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/9243e280-9db4-4411-8bac-9c265a69b94b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.