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
203 stars 122 forks source link

docker_swarm_info crashes when services=true and jobs are present #1002

Open felixfontein opened 23 hours ago

felixfontein commented 23 hours ago

Copied from https://github.com/ansible-collections/community.docker/issues/28#issuecomment-2511217556

This is also an issue for docker_swarm_info

The code is currently checking for only Replicated and Global as the modes for a service. These are however not the only modes that exists anymore, there is also ReplicatedJob and GlobalJob. When either of those types of services exists at the moment, then docker_swarm_info fails.

https://github.com/ansible-collections/community.docker/blob/d8548ef55fc719b1537b99570ea2d4960cd6ba06/plugins/modules/docker_swarm_info.py#L316

Traceback:

Traceback (most recent call last):
  File "<stdin>", line 107, in <module>
  File "<stdin>", line 99, in _ansiballz_main
  File "<stdin>", line 47, in invoke_module
  File "/usr/lib/python3.8/runpy.py", line 207, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib/python3.8/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib/python3.8/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/tmp/ansible_community.docker.docker_swarm_info_payload_492jgky3/ansible_community.docker.docker_swarm_info_payload.zip/ansible_collections/community/docker/plugins/modules/docker_swarm_info.py", line 388, in <module>
  File "/tmp/ansible_community.docker.docker_swarm_info_payload_492jgky3/ansible_community.docker.docker_swarm_info_payload.zip/ansible_collections/community/docker/plugins/modules/docker_swarm_info.py", line 376, in main
  File "/tmp/ansible_community.docker.docker_swarm_info_payload_492jgky3/ansible_community.docker.docker_swarm_info_payload.zip/ansible_collections/community/docker/plugins/modules/docker_swarm_info.py", line 236, in __init__
  File "/tmp/ansible_community.docker.docker_swarm_info_payload_492jgky3/ansible_community.docker.docker_swarm_info_payload.zip/ansible_collections/community/docker/plugins/modules/docker_swarm_info.py", line 273, in get_docker_items_list
KeyError: 'Mode'

Example of parial output from docker service inspect of ReplicatedJob:

[
    {
        "Spec": {
            "Mode": {
                  "ReplicatedJob": {
                      "MaxConcurrent": 1,
                      "TotalCompletions": 1
                  }
              },
        }
    }
]
felixfontein commented 23 hours ago

The crash is caused by https://github.com/ansible-collections/community.docker/blob/d8548ef55fc719b1537b99570ea2d4960cd6ba06/plugins/modules/docker_swarm_info.py#L273 assuming that Mode is always set. Changing that line to if item_record.get('Mode') == 'Global': should fix the issue.

felixfontein commented 15 hours ago

Fix: #1003