18

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?

techraf
  • 64,883
  • 27
  • 193
  • 198
danday74
  • 52,471
  • 49
  • 232
  • 283

5 Answers5

63

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
  ansible.builtin.file:
    path: '{{ templates_destination }}/{{ item.path }}'
    state: directory
  with_community.general.filetree: '{{ templates_source }}'
  when: item.state == 'directory'

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

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

Community
  • 1
  • 1
techraf
  • 64,883
  • 27
  • 193
  • 198
14

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.

Community
  • 1
  • 1
danday74
  • 52,471
  • 49
  • 232
  • 283
  • 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 ;) – danday74 Jan 16 '17 at 10:11
  • 3
    Why do you need to pre-create the directories if "It auto creates those directories for you at dest" – blueFast Mar 14 '18 at 14:25
  • Question: Does `with_filetree` also expect files to live within the templates folder? – sudo soul Jun 06 '19 at 17:49
  • 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. – sudo soul Jun 06 '19 at 19:06
  • does with_filetree overwrite directories if they already exist? – miller the gorilla Mar 24 '23 at 18:51
7

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'
Samuryte
  • 136
  • 1
  • 5
3

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') }}"
Sunil Shakya
  • 8,097
  • 2
  • 17
  • 20
1

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' }
psicopante
  • 353
  • 3
  • 5