Ansible 1.6> using with_first_found in a with_items loop?

Is it possible to use with_first_found in a with_items loop, for example:

 - template: dest=/foo/{{ item.name }}-{{ item.branch | default('master') }} src={{ item }} with_first_found: - {{ item.name }}-{{ item.branch | default('master') }} - {{ item.name }}.j2 - apache_site.j2 with_items: apache_sites 

It does not seem to work using with_nested .

+7
ansible ansible-playbook
source share
2 answers

Combined loops are not supported, but you can use them as searches:

 vars: site_locations: - {{ item.name }}-{{ item.branch | default('master') }} - {{ item.name }}.j2 - apache_site.j2 tasks: - template: dest=/foo/{{ item.name }}-{{ item.branch | default('master') }} src={{ lookup('first_found', site_locations }} with_items: apache_sites 
+4
source share

I had a similar need for tc Server (tomcat). This is what I did:

  • I put the configuration for a specific site in a separate task file (configure-sites.yml):

     - template: src: "{{ item }}" dest: /foo/{{ apache_site.name }}-{{ apache_site.branch | default('master') }} with_first_found: - "{{ apache_site.name }}-{{ apache_site.branch | default('master') }}" - "{{ apache_site.name }}.j2" - apache_site.j2 
  • In a separate task file, I included this task file, passing it to each site:

     - include: configure-sites.yml with_items: "{{ apache_sites }}" loop_control: loop_var: apache_site 

This uses loop_control , which requires Ansible 2.1+: http://docs.ansible.com/ansible/playbooks_loops.html#loop-control

In case this helps, you can see exactly what I did here:
https://github.com/bmaupin/ansible-role-tcserver/blob/master/tasks/main.yml
https://github.com/bmaupin/ansible-role-tcserver/blob/master/tasks/configure-instances.yml

0
source share

All Articles