Can the templates module handle multiple templates / directories?

21,184

Solution 1

The template module itself runs the action on a single file, but you can use with_filetree to loop recursively over a specified path:

- name: Ensure directory structure exists
  file:
    path: '{{ templates_destination }}/{{ item.path }}'
    state: directory
  with_filetree: '{{ templates_source }}'
  when: item.state == 'directory'

- name: Ensure files are populated from templates
  template:
    src: '{{ item.src }}'
    dest: '{{ templates_destination }}/{{ item.path }}'
  with_filetree: '{{ templates_source }}'
  when: item.state == 'file'

And for templates in a single directory you can use with_fileglob.

Solution 2

This answer provides a working example of the approach laid down by @techraf

with_fileglob expects only files to live within the templates folder - see https://serverfault.com/questions/578544/deploying-a-folder-of-template-files-using-ansible

with_fileglob will only parse files in the templates folder

with_filetree maintains the directory structure when moving the template files to dest. It auto creates those directories for you at dest.

with_filetree will parse all files in the templates folder and nested directories

- name: Approve certs server directories
  file:
    state: directory
    dest: '~/{{ item.path }}'
  with_filetree: '../templates'
  when: item.state == 'directory'

- name: Approve certs server files
  template:
    src: '{{ item.src }}'
    dest: '~/{{ item.path }}'
  with_filetree: '../templates'
  when: item.state == 'file'

Essentially, think of this approach as copying and pasting a directory and all its contents from A to B and whilst doing so, parsing all templates.

Solution 3

I could not manage to do it with the other answers. This is what worked for me:

- name: Template all the templates and place them in the corresponding path
  template:
    src: "{{ item.src }}"
    dest: "{{ destination_path }}/{{ item.path | regex_replace('\\.j2$', '') }}"
    force: yes
  with_filetree: '{{ role_path }}/templates'
  when: item.state == 'file'

Solution 4

In my case folder contain both files and jinja2 templates.

- name: copy all directories recursively
  file: dest={{templates_dest_path}}/{{ item|replace(templates_src_path+'/', '') }}  state=directory       
  with_items: "{{ lookup('pipe', 'find '+ templates_src_path +'/ -type d').split('\n') }}"

- name: copy all files recursively
  copy: src={{ item }} dest={{templates_dest_path}}/{{ item|replace(templates_src_path+'/', '') }} 
  with_items: "{{ lookup('pipe', 'find '+ templates_src_path +'/  -type f -not -name *.j2 ').split('\n') }}"
  
- name: copy templates files recursively
  template: src={{ item }} dest={{templates_dest_path}}/{{ item|replace(templates_src_path+'/', '')|replace('.j2', '') }}  
  with_items: "{{ lookup('pipe', 'find '+ templates_src_path +'/*.j2 -type f').split('\n') }}"

Solution 5

I did it and it worked. \o/

- name: "Create file template"
  template:
    src: "{{ item.src }}"
    dest: "{{ your_dir_remoto }}/{{ item.dest }}"
  loop:
    - { src: '../templates/file1.yaml.j2', dest: 'file1.yaml' }
    - { src: '../templates/file2.yaml.j2', dest: 'file2.yaml' }
Share:
21,184
danday74
Author by

danday74

Front end Angular freelancer / contractor / consultant. I am focused on front-end development, particularly NodeJS and Angular. I do like to play a little with closely linked technologies such as NGINX and Docker. I am motivated by my love for God and His command to work hard even when no one is looking. I believe in God's coming judgement and I thank God for the mercy freely available in Jesus Christ for all those who repent and receive Him.

Updated on August 05, 2021

Comments

  • danday74
    danday74 almost 3 years

    I believe the Ansible copy module can take a whole bunch of "files" and copy them in one hit. This I believe can be achieved by copying a directory recursively.

    Can the Ansible template module take a whole bunch of "templates" and deploy them in one hit? Is there such a thing as deploying a folder of templates and applying them recursively?

  • danday74
    danday74 over 7 years
    this answer is beyond useful, this answer is awesome! i've added my own answer to explain why (but have accepted this)
  • danday74
    danday74 over 7 years
    only issue i had here was with the favicon which i had to copy over manually (it was corrupted when this did it) - you may argue why did i have a favicon in my templates folder ;)
  • Dejay Clayton
    Dejay Clayton almost 7 years
    I love it when a plan comes together!
  • blueFast
    blueFast about 6 years
    Why do you need to pre-create the directories if "It auto creates those directories for you at dest"
  • sudo soul
    sudo soul almost 5 years
    Question: Does with_filetree also expect files to live within the templates folder?
  • sudo soul
    sudo soul almost 5 years
    Update: Just tested this and can confirm that with_filetree WILL WORK with any directory! So, it is not limited to just a templates directory.
  • Cyril
    Cyril over 2 years
    It seems with_filetree is not available anymore in ansible 2.11
  • Samuryte
    Samuryte about 2 years
    @Cyril just tested with ansible v2.12.1 and it worked perfectly