# Deploy directory hierarchy of templates

**URL:** <https://forum.ansible.com/t/deploy-directory-hierarchy-of-templates/31639>\
**Category:** Ansible Project\
**Created:** [November 12, 2019, 7:05am UTC](https://forum.ansible.com/t/deploy-directory-hierarchy-of-templates/31639 "2019-11-12T07:05:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 12, 2019, 7:05am UTC](https://forum.ansible.com/t/deploy-directory-hierarchy-of-templates/31639/1 "2019-11-12T07:05:00Z")

</div>

Hi

I’m looking to deploy a number of configuration files using the template module. The files are structured in a directory hierarchy that needs to be maintained. So for this purpose I want to use the with\_filetree lookup as mentioned on  
[https://docs.ansible.com/ansible/latest/plugins/lookup/filetree.html](https://docs.ansible.com/ansible/latest/plugins/lookup/filetree.html).

This is (at least) a two step process whereby the directory structure is created first, and the actual templates are done next.  
The example also lists a third task that recreates symlinks if one needs that.

While this probably works fine, I do see an issue when a template is deployed first and then removed from the local directory later.  
Redeployment would not remove this ‘orphan’ file from the target directory which will cause issues with my application.

So I think a final ‘cleanup’ step would be needed that deletes any files on the target that aren’t in the local directory.

Before I do any wheel reinvention - does someone already have such a task at hand?

Thx

Dick

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [November 12, 2019, 12:33pm UTC](https://forum.ansible.com/t/deploy-directory-hierarchy-of-templates/31639/2 "2019-11-12T12:33:50Z")

</div>

OK to answer my own question, it wasn't very trivial, but I got it to  
work the way I wanted.  
Entire playbook:

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 12, 2019, 2:29pm UTC](https://forum.ansible.com/t/deploy-directory-hierarchy-of-templates/31639/3 "2019-11-12T14:29:13Z")

</div>

I once had a similar problem and I solved it in also a similar way,  
then made it into a role [https://galaxy.ansible.com/bcoca/tidy](https://galaxy.ansible.com/bcoca/tidy)

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [September 21, 2020, 5:28pm UTC](https://forum.ansible.com/t/deploy-directory-hierarchy-of-templates/31639/4 "2020-09-21T17:28:54Z")

</div>

Sorry for resurrecting this old thread.  
I shuffled things around a bit lately, and came up with a single  
playbook file that can be used to template a directory tree. See  
below.
