# Recursively Copy DIrectories with files that contain variables

**URL:** <https://forum.ansible.com/t/recursively-copy-directories-with-files-that-contain-variables/7303>\
**Category:** Get Help\
**Created:** [July 16, 2024, 9:25pm UTC](https://forum.ansible.com/t/recursively-copy-directories-with-files-that-contain-variables/7303 "2024-07-16T21:25:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ekim1975](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/ekim1975/32/3629_2.png) [@ekim1975](https://forum.ansible.com/u/ekim1975)\
**Post date:** [July 16, 2024, 9:25pm UTC](https://forum.ansible.com/t/recursively-copy-directories-with-files-that-contain-variables/7303/1 "2024-07-16T21:25:41Z")

</div>

So I have under my local “files” ansible folder, directories and files that are the same structure that I want on my target Linux host.

ansible.builtin.copy nicely recursively creates all sub directories and files under the “dest:” that I specify. The problem is that some of the files contain variables. As I know now, the copy module only copies as-is.

The ansible.builtin.template module will replace the {{ my\_var }} with the value, but it seems like I have have a module for each and every file that contains a variable.

Is there some combination of the “copy” and “template” module I can use that will recursively copy all directories and files but ALSO replace all of the {{ vars }} in the files that have them?

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [July 17, 2024, 7:58pm UTC](https://forum.ansible.com/t/recursively-copy-directories-with-files-that-contain-variables/7303/2 "2024-07-17T19:58:42Z")

</div>

`template` cannot do recursive actions like your suggesting. You could loop over the files though

```auto
- name: foo
  ansible.builtin.template:
    src: "{{ item }}"
    dest: /tmp/{{ item }}
  loop:
    - bizz/buzz.yml
    - bizz/baz.yml
    - fizz.yml

```

You can pair this with the fileglob lookup if you have a lot of files but it gets messy IMO. Something like this, maybe:

```auto
- name: foo
  ansible.builtin.template:
    src: "{{ item }}"
    dest: /tmp/{{ item | replace(playbook_dir + '/templates/', '') }}
  with_fileglob:
    - {{ playbook_dir }}/templates/bizz/*.yml"

```

---

<div class="post-metadata">

**Author:** ![dnmvisser](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@dnmvisser](https://forum.ansible.com/u/dnmvisser)\
**Post date:** [August 19, 2024, 2:34pm UTC](https://forum.ansible.com/t/recursively-copy-directories-with-files-that-contain-variables/7303/3 "2024-08-19T14:34:07Z")

</div>

I had a need for this on several occasions, I ended up creating a role for this:

> **[GitHub - dnmvisser/ansible\_role\_template\_tree: Ansible role to deploy an entire tree of templates](https://github.com/dnmvisser/ansible_role_template_tree)**
>
> Ansible role to deploy an entire tree of templates
