ansible 1.6 > using with

Is it possible to use with_first_found in a with_items loop such as:

- 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

Can't seem to make it work using with_nested .


组合循环不受支持,但可以将它们用作查找:

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

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

  • I put the site-specific configuration in a separate tasks 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
    
  • From a separate tasks file I included that tasks file, passing it each site:

    - include: configure-sites.yml
      with_items: "{{ apache_sites }}"
      loop_control:
        loop_var: apache_site
    
  • This makes use of loop_control which requires Ansible 2.1+: http://docs.ansible.com/ansible/playbooks_loops.html#loop-control

    In case it 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

    链接地址: http://www.djcxy.com/p/78394.html

    上一篇: 在ComboBox中实现IDataErrorInfo

    下一篇: 可靠的1.6>使用