ansible - 根据另一个变量定义var的值

时间:2017-06-27 13:16:59

标签: docker ansible ansible-2.x

今天我有一个允许我启动多个docker容器的循环

- name: start container current
      docker_container:
        name: "{{ item.name }}"
        image: "{{ item.name }}:{{ item.version }}"
        state: started
        recreate: true
        ports:
          - "{{ item.ports }}"
        volumes:
          - /opt/application/i99/{{ item.type }}/logs:/opt/application/i99/{{ item.type }}/logs
        env_file: /opt/application/i99/{{ item.type }}/{{ item.name }}/{{ item.name }}-PreProd-config.list
        env:
            LOG_FILE_WS: "/opt/application/i99/{{ item.type }}/logs/{{ hostname }}_WS.log"
      with_items:       
         - { name: 'backend', ports: '8000:8000', type: 'current', version: '{{RCD_VERSION_CURRENT}}' }
         - { name: 'connecteur', ports: '8400:8400', type: 'current', version: '{{RCD_VERSION_CURRENT}}' }
         - { name: 'api-alerting', ports: '8100:8100', type: 'current', version: '{{RCD_VERSION_CURRENT}}' }
         - { name: 'api-tracking', ports: '8200:8200', type: 'current', version: '{{RCD_VERSION_CURRENT}}' }

我有一个额外的变量{{ RCD_APIS }},其中包含我所有容器名称的列表。我想循环遍历该列表并在条件下定义以下变量并运行容器

vars定义:ports,type,version

我想做点什么

- name: start container current
      docker_container:
        name: "{{ item }}"
        image: "{{ item }}:{{ version }}"
        state: started
        user: adi99api
        recreate: true
        ports:
          - "{{ ports }}"
        volumes:
          - /opt/application/i99/{{ type }}/logs:/opt/application/i99/{{ type }}/logs
        env_file: /opt/application/i99/{{ type }}/{{ item }}/{{ name }}-PreProd-config.list
        env:
            LOG_FILE_WS: "/opt/application/i99/{{ type }}/logs/{{ hostname }}_WS.log"
    with_items: "{{ RCD_APIS.split(',') }}"
    when: ( item  == "backend", ports: '8000:8000', type: 'current', version: '{{RCD_VERSION_CURRENT}}') or
          ( item  == "connecteur", ports: '8400:8400', type: 'pilote', version: '{{RCD_VERSION_PILOTE}}') 

1 个答案:

答案 0 :(得分:0)

Template

_

# in a vars file, or a `vars` section
---
docker_containers_config:
  backend: 
    ports: '8000:8000'
    type: current
    version: '{{RCD_VERSION_CURRENT}}'
  connecteur:
    ports: '8400:8400'
    type: current
    version: '{{RCD_VERSION_CURRENT}}'
  api-alerting:
    ports: '8100:8100'
    type: 'current'
    version: '{{RCD_VERSION_CURRENT}}'
  api-tracking:
    ports: '8200:8200'
    type: 'current'
    version: '{{RCD_VERSION_CURRENT}}'