How can I pass variables in Ansible to Docker-compose file

94 Views Asked by At

I am using ansible to deploy 7 containers(7 tasks running consecutively) I have a docker compose file the problem I am facing occurs at the point when i expect my variable in the playbook 'tel' to be used in the docker compose file when mounting volume from host to the container; this does not happen and the task fails.

Please how can I pass the 'tel' variable value in the compose file that will allow the container create successfully at the corresponding task

here is my playbook:

---
- hosts: mg_all
  gather_facts: yes
  become: yes
  become_method: sudo
  run_once: true

  tasks:
    # intal natve vens 
    - name: deploy stack int_na_fao
      include_role:
        name: docker_stack_telrf_net_ronse
      vars:
        tel: int_na_fao

    # exnal natve vens 
    - name: deploy stack for ext_na_fao
      include_role:
        name: docker_stack_telrf_net_ronse
      vars:
        tel: ext_na_fao

..... till the seventh

here is my docker compose file

---
version: '3.8'
services:
  tel:
    image: tro.azurecr.io/dor.io/telegraf:1.2.0
    networks:
      - external
    volumes:
      - "/etc/${tel}/telaf_ca_mas.cer:/etc/tel/ca_mas.cer"
     - "/etc/${tel}/telaf_${conf_name}telegraf.conf:/etc/tel/tel.conf"
    logging:
      driver: "json-file"
      options:
        max-file: "3"
        max-size: 100m
    restart: always

volumes:
  tel-volume:

networks:
  external:

I have tried to use the {{ tel }} in the compose file however this didnt insert the value of tel in the file; when I tried to insert directly the value of tel in the compose file for example value of tel in the first task "int_na_fao" this worked but the problem is that the container created was the same and didnt use the actual file specific for the task I needed

1

There are 1 best solutions below

7
Ouss On

Use environment to set the remote environment, as in this example from Ansible documentation:

- hosts: all
  remote_user: root
  tasks:
    - name: Install cobbler
      ansible.builtin.package:
        name: cobbler
        state: present
      environment:
        http_proxy: http://proxy.example.com:8080

In you code, you use, include_role. Because of that we need to pass the environment slightly differently using apply. So back to your code:

---
- hosts: mg_all
  gather_facts: yes
  become: yes
  become_method: sudo
  run_once: true

  tasks:
    # intal natve vens 
    - name: deploy stack int_na_fao
      include_role:
        name: docker_stack_telrf_net_ronse
        apply:
            environment:
                tel: int_na_fao

    # exnal natve vens 
    - name: deploy stack for ext_na_fao
      include_role:
        name: docker_stack_telrf_net_ronse
        apply: 
            environment:
                tel: ext_na_fao

That is described here in the ansible documentation of include_role here: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/include_role_module.html and notice that apply was added in Ansible 2.7