Deploying a folder of template files using ansible

Solution 1:

You could use with_fileglob to get the list of files from your template directory and use filters to strip the j2 extension like this:

- name: create x template
    src: "{{ item }}"
    dest: /tmp/{{ item | basename | regex_replace('\.j2$', '') }}
    - ../templates/*.j2

Solution 2:

Michael DeHaan(creator of Ansible) made a post on CoderWall that talks about very similar issue. You can adjust and expand it according to your needs(such as permissions and ownership). Relevant part of the post is here:

This can be simplified by using "with_items" and a single notify statement. If any of the tasks change, the service will be notified in exactly the same way that it needs to restart at the end of the playbook run.

 - name:  template everything for fooserv
   template: src={{item.src}} dest={{item.dest}}
      - { src: 'templates/foo.j2', dest: '/etc/splat/foo.conf' }
      - { src: 'templates/bar.j2', dest: '/etc/splat/bar.conf' }
      - restart fooserv

Note that since we have tasks that take more than one unique argument, we don't just say "item" in the 'template:' line, but use with_items with a hash (dictionary) variable. You can also keep it a little shorter by using lists, if you like. This is a stylistic preference:

 - name:  template everything for fooserv
   template: src={{item.0}} dest={{item.1}}
      - [ 'templates/foo.j2', '/etc/splat/foo.conf' ]
      - [ 'templates/bar.j2', '/etc/splat/bar.conf' ]
      - restart fooserv

Of course we could also define the list you were walking over in another file, like a "groupvars/webservers" file to define all the variables needed for the webservers group, or a YAML file loaded from the "varsfiles" directive inside the playbook. Look how this can clean up if we do.

- name: template everything for fooserv
  template: src={{item.src}} dest={{item.dest}}
  with_items: {{fooserv_template_files}}
      - restart fooserv

Solution 3:

The answer by Russel does work but it needs improvement

- name: create x template
- template: src={{ item }} dest=/tmp/{{ item | basename | regex_replace('.j2','') }}
- with_fileglob:
   - files/*.j2

Firs of all the $ needs to go as it was wrong regex in the regex_replace . Secondly all the files should be in the files directory rather than templates directory

Solution 4:

I wrote a filetree lookup plugin that can help with actions on file trees.

  • (Ansible v2.x)
  • (Ansible v1.9.x)

You can recurse over files in a file tree and based on file properties perform actions (e.g. template or copy). Since the relative path is returned, you can recreate the filetree on the target system(s) with ease.

- name: Template complete tree
    src: '{{ item.src }}'
    dest: /web/{{ item.path }}
    force: yes
  with_filetree: some/path/
  when: item.state == 'file'

It makes for more readable playbooks.

Solution 5:

The below command worked for me to do a recursive lookup for j2 files in templates and move it to the destination. Hope it helps someone looking for recursive copy of templates to destination.

     - name: Copying the templated jinja2 files
       template: src={{item}} dest={{RUN_TIME}}/{{ item | regex_replace(role_path+'/templates','') | regex_replace('\.j2', '') }}
       with_items: "{{ lookup('pipe','find {{role_path}}/templates -type f').split('\n') }}"