Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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
  template:
    src=files/x.conf.j2
    dest=/tmp/x.conf
    owner=root
    group=root
    mode=0755
  notify:
    - restart myService
share|improve this question

3 Answers 3

up vote 4 down vote accepted

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}
   with_items:
      - { src: 'templates/foo.j2', b: '/etc/splat/foo.conf' }
      - { src: 'templates/bar.j2', b: '/etc/splat/bar.conf' }
   notify: 
      - 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}
   with_items:
      - [ 'templates/foo.j2', '/etc/splat/foo.conf' ]
      - [ 'templates/bar.j2', '/etc/splat/bar.conf' ]
   notify: 
      - 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}
  notify: 
      - restart fooserv
share|improve this answer

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 }}
  with_items:
  - "{{ template_files.stdout.splitlines() }}"
share|improve this answer

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:
  - files/*.j2
share|improve this answer

Your Answer

 
discard

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.