ansible – 根据另一个variables定义var的值

今天我有一个循环,允许我启动多个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}}' } 

我有一个额外的variables{{ RCD_APIS }} ,其中包含我所有容器名称的列表。 我想遍历该列表并定义以下variablesconditionnally名称并运行容器

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}}') 

 # 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}}' 

_

 # In your tasks - name: start container current docker_container: name: "{{ item }}" image: "{{ item }}:{{ docker_containers_config[item].version }}" state: started recreate: true ports: - "{{ docker_containers_config[item].ports }}" volumes: - /opt/application/i99/{{ item.type }}/logs:/opt/application/i99/{{ item.type }}/logs env_file: /opt/application/i99/{{ docker_containers_config[item].type }}/{{ item }}/{{ item }}-PreProd-config.list env: LOG_FILE_WS: "/opt/application/i99/{{ docker_containers_config[item].type }}/logs/{{ hostname }}_WS.log" with_items: "{{ RCD_APIS.split(',') }}"