I like to use the synchronize module to configure /etc/nginx/sites-enabled/
this way, when using it with delete=yes my ansible setup would always be the whole truth about the nginx setup, even if I add and especially delete virtual hosts in ansible. (now I’m using template: or copy: but obsolete files are not deleted then).
But my nginx config files are templates in ansible, not plain files, so for me it’ll be more useful to have a synchronize-like module, or an option to use templates.
is this possible/doable? on the todo list?
I use 2 tasks to do this:
#1 runs before template:
shell: ls /etc/nginx/sites-enabled/*.conf
register: nginx_existing
then i run my template and register the items nginx_templates
then i run a
file: path=“{{item}}” state=absent
with_items: nginx_existing|difference(nginx_templates)
this will remove files that appear in the nginx_existing list that don’t appear in the nginx_templates list
(the code above is not exact, there is some extra referencing to be done in the vars but you get the idea).
A ‘quicker’ solution is to have task #1 just delete everything and always populate from temlpates, but this hides changes as it ALWAYS changes everything.
That’s a pretty neat trick!
BTW, rather than using the “ls” command, I’d recommend using the “stat” module for that purpose, since it’s a little more explicit and dedicated to the task.
http://ansibleworks.com/docs/modules.html#stat
run with “-v” to see all the data that it returns.