ansible-collections / community.docker

Community Docker Collection for Ansible: modules and plugins for working with Docker
https://galaxy.ansible.com/ui/repo/published/community/docker/
GNU General Public License v3.0
200 stars 115 forks source link

'dict object' has no attribute 'services' when using `docker_compose_v2` #912

Closed ShaneMcC closed 3 months ago

ShaneMcC commented 4 months ago
SUMMARY

When using docker_compose_v2 with register: output it is no longer possible (despite documentation suggesting otherwise in the examples) to test against the value of output.services.service.container.state.running because services is no longer populated in the output dict.

output.containers exists, but this is just a list so can not be tested against as easily because the container name is not used as a key.

ISSUE TYPE
COMPONENT NAME

docker_compose_v2

ANSIBLE VERSION
ansible [core 2.16.3]
  config file = /home/shane/.ansible.cfg
  configured module search path = ['/home/shane/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /home/shane/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/local/bin/ansible
  python version = 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
COLLECTION VERSION
# /home/shane/.ansible/collections/ansible_collections
Collection       Version
---------------- -------
community.docker 3.10.4

# /usr/lib/python3/dist-packages/ansible_collections
Collection       Version
---------------- -------
community.docker 3.7.0
CONFIGURATION
ANSIBLE_NOCOWS(/home/shane/.ansible.cfg) = True
CONFIG_FILE() = /home/shane/.ansible.cfg
OS / ENVIRONMENT

Linux

STEPS TO REPRODUCE
- name: Debug
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Run container with docker-compose.
      community.docker.docker_compose_v2:
        state: present
        pull: always
        remove_orphans: true
        project_name: ansibledemo
        wait: true
        definition:
          services:
            demo:
              image: traefik/whoami:latest
              restart: always
      register: output

    - name: Check services are running
      ansible.builtin.assert:
        that:
          - "output.services.ansibledemo.ansibledemo-demo-1.state.running"
EXPECTED RESULTS

Playbook TASK Check services are running to run successfully without errors.

ACTUAL RESULTS
TASK [Check services are running] *******************************************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'output.services.ansibledemo.ansibledemo-demo-1.state.running' failed. The error was: error while evaluating conditional (output.services.ansibledemo.ansibledemo-demo-1.state.running): 'dict object' has no attribute 'services'. 'dict object' has no attribute 'services'"}
felixfontein commented 3 months ago

Sorry, I forgot to adjust that part of the example when copying it over from the docker_compose module. I'm fixing it in #917.

I've decided to not add the services dictionary as in the docker_compose module since IMO a list is more flexible. If you want to group the containers by service, you can use output.containers | groupby("Service"). If you simply want all containers for a service, you can use output.containers | selectattr("Service", "equalto", "name-of-service"). If you want the first container, add | first.