Create a docker-compose with loop in ansible

Viewed 992

I need to build a docker compose based on a yml file. In the next yml it will be the name, the image and the version of each service.

"services":
  - "service": "front"
    "image": "acalls-caselog-web-app"
    "version": "latest" 
  - "service": "back"
    "image": "acalls-caselog-web-service"
    "version": "latest" 
  - "service": "vb"
    "image": "acalls-caselog-vb-service"
    "version": "latest" 
  - "service": "salesforce"
    "image": "acalls-caselog-salesforce-app-service"
    "version": "latest"  
  - "service": "tts"
    "image": "ydilo-tts-service"
    "version": "latest" 
  - "service": "ai classifier"
    "image": "acalls-caselog-ai-classifier-service"
    "version": "latest"  

Up to now I had an array to set each image in the docker compose, like this

version: "3.3"
services:
  front:
    image: url/{{services[0].image}}:{{services[0].version}}
    ports: 
      - "81:81"
    extra_hosts:
      - "backend:172.32.3.46" 
    environment:
      profile: preproduction
  back:
    image: url/{{services[1].image}}:{{services[1].version}}
    ports: 
      - "20101:20101"
    environment:
      profile: preproduction
  saleforce:
    image: url/{{services[2].image}}:{{services[2].version}}
    ports:
      - "20103:20103"
    environment:
      profile: preproduction

But I need to find a way to make this dynamically with a loop in the ansible task, for example, without the array position in the docker-compose file.

Main.yml

---
  - name: stop container
    ignore_errors: yes
    become: True
    shell:
      cmd: "docker-compose down"
      chdir: dir

  - name: set docker-compose
    template:
      src: docker-compose-acalls.yml.j2
      dest: dir/docker-compose.yml
      mode: 0700

  - name: Run container
    become: True
    shell:
      cmd: "nohup docker-compose -f docker-compose.yml up -d"
      chdir: dir
1 Answers

you create a template file: you have to play with whitespace , %- and -% to adjust the position, i have just given the general idea

version: "3.3"
services:
{% for item in services %}
  {{ item.service }}:
{% if item.image is defined %}
    image: url/{{item.image}}:{{item.version}} 
{%- endif %}     
{% if item.ports is defined %}
    ports: 
      - "{{ item.ports[0] }}"
{%- endif %}
{% if item.extra_hosts is defined %} 
    extra_hosts:
      - "{{ item.extra_hosts[0] }}"
{%- endif %}
{% if item.environment is defined %} 
    environment:
      - profile: {{ item.environment.profile }}
{% endif %}
{% endfor %}

your playbook:

- name: test
  hosts: localhost
  vars_files:
    - reference.yml   
  tasks:
    template:
      src: fileconf.j2
      dest: composedocker.yml

and your reference.yml file:

services:
  - service: "front"
    image: "acalls-caselog-web-app"
    ports: 
      - "81:81"
    extra_hostss:
      - "backend:172.32.3.46" 
    environment:
      profile: preproduction
    version: "latest"
  - service: "back"
    image: "acalls-caselog-web-service"
    version: "latest"
    ports: 
      - "20101:20101"
    environment:
      profile: preproduction    
  - service: "vb"
    image: "acalls-caselog-vb-service"
    version: "latest"
  - service: "salesforce"
    image: "acalls-caselog-salesforce-app-service"
    version: "latest"
    ports:
      - "20103:20103"
    environment:
      profile: preproduction
  - service: "tts"
    image: "ydilo-tts-service"
    version: "latest"
  - service: "ai classifier"
    image: "acalls-caselog-ai-classifier-service"
    version: "latest"

result:

version: "3.3"
services:
  front:
    image: url/acalls-caselog-web-app:latest     
    ports: 
      - "81:81" 
    extra_hosts:
      - "backend:172.32.3.46" 
    environment:
      - profile: preproduction
  back:
    image: url/acalls-caselog-web-service:latest     
    ports: 
      - "20101:20101" 
    environment:
      - profile: preproduction
  vb:
    image: url/acalls-caselog-vb-service:latest     
  salesforce:
    image: url/acalls-caselog-salesforce-app-service:latest     
    ports: 
      - "20103:20103" 
    environment:
      - profile: preproduction
  tts:
    image: url/ydilo-tts-service:latest     
  ai classifier:
    image: url/acalls-caselog-ai-classifier-service:latest    
Related