2014-03-26 88 views
7

是否有可能在一個with_items循環使用with_first_found如:ansible 1.6>在with_items循環中使用with_first_found?

- 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 

似乎無法使其工作使用with_nested

+0

看起來毛茸茸的,什麼是你想實現什麼? – Rico

回答

4

結合的循環是不支持的,但是你可以用它們作爲查找:

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 
0

我對TC服務器(Tomcat)的類似需求。這是我做過什麼:

  1. 我把特定網站的配置在一個單獨的任務文件(配置-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 
    
  2. 從一個單獨的任務文件,我包括這任務文件,通過它每個站點:

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

這使得利用loop_control這就需要nsible 2.1+:http://docs.ansible.com/ansible/playbooks_loops.html#loop-control

如果有幫助,你可以看到正是我在這裏做:
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