From 132f5cd0cae8a81ff130253910bd14ed99f597d2 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 27 Dec 2022 15:11:43 +0100 Subject: [PATCH 1/2] Allow to list all containers. --- .../538-docker_host_info-all-containers.yml | 2 + plugins/modules/docker_host_info.py | 10 ++++- .../docker_host_info/tasks/test_host_info.yml | 38 ++++++++++++++++++- 3 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/538-docker_host_info-all-containers.yml diff --git a/changelogs/fragments/538-docker_host_info-all-containers.yml b/changelogs/fragments/538-docker_host_info-all-containers.yml new file mode 100644 index 000000000..87b4b834b --- /dev/null +++ b/changelogs/fragments/538-docker_host_info-all-containers.yml @@ -0,0 +1,2 @@ +minor_changes: + - "docker_host_info - allow to list all containers with new option ``container_all`` (https://github.com/ansible-collections/community.docker/issues/535, https://github.com/ansible-collections/community.docker/pull/538)." diff --git a/plugins/modules/docker_host_info.py b/plugins/modules/docker_host_info.py index 0d452ba80..63d235e84 100644 --- a/plugins/modules/docker_host_info.py +++ b/plugins/modules/docker_host_info.py @@ -44,6 +44,13 @@ - Whether to list containers. type: bool default: false + containers_all: + description: + - By default, only running containers are returned. + - This corresponds to the C(--all) option to C(docker container list). + type: bool + default: false + version_added: 3.4.0 containers_filters: description: - A dictionary of filter values used for selecting containers to list. @@ -283,7 +290,7 @@ def get_docker_items_list(self, docker_object=None, filters=None, verbose=False) if docker_object == 'containers': params = { 'limit': -1, - 'all': 0, + 'all': 1 if self.client.module.params['containers_all'] else 0, 'size': 0, 'trunc_cmd': 0, 'filters': convert_filters(filters) if filters else None, @@ -336,6 +343,7 @@ def get_docker_items_list(self, docker_object=None, filters=None, verbose=False) def main(): argument_spec = dict( containers=dict(type='bool', default=False), + containers_all=dict(type='bool', default=False), containers_filters=dict(type='dict'), images=dict(type='bool', default=False), images_filters=dict(type='dict'), diff --git a/tests/integration/targets/docker_host_info/tasks/test_host_info.yml b/tests/integration/targets/docker_host_info/tasks/test_host_info.yml index f7ec8b2c3..4dcee3a14 100644 --- a/tests/integration/targets/docker_host_info/tasks/test_host_info.yml +++ b/tests/integration/targets/docker_host_info/tasks/test_host_info.yml @@ -6,10 +6,11 @@ - name: Create random container/volume name set_fact: cname: "{{ 'ansible-docker-test-%0x' % ((2**32) | random) }}" + cname2: "{{ 'ansible-docker-test-%0x' % ((2**32) | random) }}" vname: "{{ 'ansible-docker-test-%0x' % ((2**32) | random) }}" - debug: - msg: "Using container name '{{ cname }}' and volume name '{{ vname }}'" + msg: "Using container names '{{ cname }}' and '{{ cname2 }}', and volume name '{{ vname }}'" - block: - name: Get info on Docker host @@ -30,7 +31,7 @@ # * container and volume lists are non-emtpy because of the created objects; # * image list is non-empty because the image of the container is there; # * network list is always non-empty (default networks). - - name: Create container + - name: Create running container docker_container: image: "{{ docker_test_image_alpine }}" command: '/bin/sh -c "sleep 10m"' @@ -41,9 +42,20 @@ state: started register: container_output + - name: Create running container + docker_container: + image: "{{ docker_test_image_alpine }}" + name: "{{ cname2 }}" + labels: + key2: value2 + key3: value3 + state: stopped + register: container2_output + - assert: that: - container_output is changed + - container2_output is changed - name: Create a volume docker_volume: @@ -108,6 +120,28 @@ assert: that: "{{ output.containers | length }} == 0" + - name: Get info on Docker host and list containers matching filters (single label, not all containers) + docker_host_info: + containers: true + containers_all: false + containers_filters: + label: key2=value2 + register: output + + - name: Get info on Docker host and list containers matching filters (single label, all containers) + docker_host_info: + containers: true + containers_all: true + containers_filters: + label: key2=value2 + register: output_all + + - name: assert one resp. two container is returned + assert: + that: + - "{{ output.containers | length }} == 1" + - "{{ output_all.containers | length }} == 2" + - name: Get info on Docker host and list containers with verbose output docker_host_info: containers: yes From d9cb2bee68705caf3a1085470e0d2197d4dffee4 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 27 Dec 2022 15:13:47 +0100 Subject: [PATCH 2/2] Fix typo. --- changelogs/fragments/538-docker_host_info-all-containers.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelogs/fragments/538-docker_host_info-all-containers.yml b/changelogs/fragments/538-docker_host_info-all-containers.yml index 87b4b834b..99b8b1136 100644 --- a/changelogs/fragments/538-docker_host_info-all-containers.yml +++ b/changelogs/fragments/538-docker_host_info-all-containers.yml @@ -1,2 +1,2 @@ minor_changes: - - "docker_host_info - allow to list all containers with new option ``container_all`` (https://github.com/ansible-collections/community.docker/issues/535, https://github.com/ansible-collections/community.docker/pull/538)." + - "docker_host_info - allow to list all containers with new option ``containers_all`` (https://github.com/ansible-collections/community.docker/issues/535, https://github.com/ansible-collections/community.docker/pull/538)."