# synchronize with templates

**URL:** <https://forum.ansible.com/t/synchronize-with-templates/14217>\
**Category:** Ansible Project\
**Created:** [December 24, 2013, 10:11pm UTC](https://forum.ansible.com/t/synchronize-with-templates/14217 "2013-12-24T22:11:27Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Damjan\_Georgievski](https://avatars.discourse-cdn.com/v4/letter/d/e19b73/32.png) [@Damjan\_Georgievski](https://forum.ansible.com/u/Damjan_Georgievski)\
**Post date:** [December 24, 2013, 10:11pm UTC](https://forum.ansible.com/t/synchronize-with-templates/14217/1 "2013-12-24T22:11:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 26, 2013, 7:30pm UTC](https://forum.ansible.com/t/synchronize-with-templates/14217/2 "2013-12-26T19:30:20Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [December 26, 2013, 7:52pm UTC](https://forum.ansible.com/t/synchronize-with-templates/14217/3 "2013-12-26T19:52:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [December 27, 2013, 7:14pm UTC](https://forum.ansible.com/t/synchronize-with-templates/14217/4 "2013-12-27T19:14:12Z")

</div>

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](http://ansibleworks.com/docs/modules.html#stat)

run with “-v” to see all the data that it returns.
