Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there an easy way to deploy a folder full of template .j2 folder to a linux box, using the same name as the template, but without the .j2 extension, rather than using the template module for each file?

Right now i have a long list of:

- name: create x template
    - restart myService
share|improve this question
up vote 30 down vote accepted

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
- template: src={{ item }} dest=/tmp/{{ item | basename | regex_replace('\.j2','') }}
- with_fileglob:
  - ../templates/*.j2
share|improve this answer
@Trololololol This should be an accepted answer, I suppose. – Vladislav Rastrusny May 1 '15 at 22:37
note with_fileglob always operates from files/, you can get to templates with ../templates/mytemplate/*. – ThorSummoner May 13 '15 at 20:10
Thanks, this is super helpful. I found that I had to use two backslashes to escape the literal period in the regex_replace function. Maybe because I had the entire dest template portion within double quotes so I could use YAML format for the task definition (which I prefer over the one-liner format). – Tony Cesaro Mar 31 at 23:03

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
share|improve this answer
A simpler method might be to write template: src=templates/{{item}}.j2 dest=/etc/splat/{{item}}.conf, and then use a plain list of items: with_items: - foo - bar – Ethan Jan 6 '15 at 3:47
This actually looks wrong now. The correct template line would be template: src={{item.src}} dest={{item.dest}} (i.e. not ${var} but rather {{var}}) – Fabiano Francesconi Nov 13 '15 at 13:45
@FabianoFrancesconi updated. – Mxx Nov 13 '15 at 15:01

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

share|improve this answer
Welcome to Server Fault! Your answer suggests a workable solution to the question is available through a previous answer, thus would be more appropriate as an edit of that answer. Please consider deleting your current answer and suggesting an edit to Russell's answer. – Paul Jan 16 '15 at 17:31

There is a possibility to grab the list of actual files from the directory automatically and iterate them afterwards..

- name:         get the list of templates to transfer
  local_action: "shell ls templates/* | sed 's~.*/~~g'"
  register:     template_files

- name:         iterate and send templates
  template:     src=templates/{{ item }} dest=/mydestination/{{ item }}
  - "{{ template_files.stdout.splitlines() }}"
share|improve this answer

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') }}"
share|improve this answer

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

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.

share|improve this answer
It's not been merged yet :-( – Morgan Christiansson May 24 at 11:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.