In Ansible, I currently have vars set out like this
container_a_version: 1
container_b_version: 6
container_c_version: 3
container_d_version: 2
...
containers:
- container_a
- container_b
- container_c
- container_d
...
Each container has its own template file which has a line like this
image: registry.sportpesa.com/sportpesa/global/container-a:{{ container_a_version }}
My playbook
- name: Copy templates
template:
src: templates/docker-compose-{{ item }}.yml
dest: /srv/docker-compose-{{ item }}.yml
loop: "{{ containers }}"
If I want to deploy only container a and c. I change my variable to this
containers:
- container_a
# - container_b
- container_c
# - container_d
What I want to do condense my variables to just have 1 var like this
containers:
- { name: a, version: 1 }
- { name: b, version: 6 }
- { name: c, version: 3 }
- { name: d, version: 2 }
However I'm not sure how I can call the container version in my template. Is this possible?
In your loop, you can call a field of the item looped like this:
name: {{ item.name }}
version: {{ item.version }}
I assume you have to put this in your scpecific context. Looks this documentation for more details about jinja templating if necessary.
Put the data into a dictionary
containers:
a: 1
b: 6
c: 3
d: 2
containers_enabled: [a, c]
and iterate enabled containers only. For example
- debug:
msg:
- "{{ src }}"
- "{{ dest }}"
- "{{ image }}"
loop: "{{ containers_enabled }}"
vars:
src: "templates/docker-compose-container_{{ item }}.yml"
dest: "/srv/docker-compose-container_{{ item }}.yml"
image: "container-{{ item }}{{ containers[item] }}"
not tested
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.