2014-03-26 83 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