[community.docker.docker_stack] How to use variables in inline compose definition?

I want to use the docker_config and the docker_stack module to update a swarm configuration used by a service.

The playbook first creates a new version of the swarm configuration with the docker_config module and then tries to update the running stack to pick up the new configuration.

The example of the docker_stack module with an inlined compose file is taken from the examples section of the official module documentation.

This is my playbook:

- name: Update swarm cluster
  hosts: camina.m.internal
  tasks:
    - name: Update Traefik configuration
      community.docker.docker_config:
        name: traefik.yaml
        debug: true
        versions_to_keep: 2
        rolling_versions: true
        data: "{{ lookup('file', 'etc/traefik/traefik.yaml') }}"
        state: present
      register: traefik_config

    - name: Update traefik service
      community.docker.docker_stack:
        name: traefik
        state: present
        compose:
          - /srv/cloud/traefik/compose.yml
          - version: '3'
            configs:
              "{{ traefik_config.config_name }}":
                name: traefik.yaml
                external: true

Unfortunately the docker_stack module seems not to resolve variables in the inline compose definition.

I get the error:

fatal: [camina.m.internal]: FAILED! => {"changed": false, "msg": "docker stack up deploy command failed", "rc": 1, "stderr": "configs Additional property {{ traefik_config.config_name }} is not allowed\n", "stderr_lines": ["configs Additional property {{ traefik_config.config_name }} is not allowed"], "stdout": "", "stdout_lines": []}

Sorry got that wrong. I mistakenly muddled up the name of the config with the config itself.

This example is working as expected:

- name: Update swarm cluster
  hosts: camina.m.internal
  tasks:
    - name: Update Traefik configuration
      community.docker.docker_config:
        name: traefik.yaml
        debug: true
        versions_to_keep: 2
        rolling_versions: true
        data: "{{ lookup('file', 'etc/traefik/traefik.yaml') }}"
        state: present
      register: traefik_config

    - name: Update traefik service
      community.docker.docker_stack:
        name: traefik
        state: present
        compose:
          - /srv/cloud/traefik/compose.yml
          - version: '3'
            configs:
              traefik.yaml:
                name: "{{ traefik_config.config_name }}"
                external: true
1 Like