docker_host_info: allow to list all containers (#538)

* Allow to list all containers.

* Fix typo.
This commit is contained in:
Felix Fontein 2022-12-27 21:39:17 +01:00 committed by GitHub
parent 44b98609fd
commit faa7fef504
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 3 deletions

View File

@ -0,0 +1,2 @@
minor_changes:
- "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)."

View File

@ -44,6 +44,13 @@ options:
- 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 @@ class DockerHostManager(DockerBaseClass):
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 @@ class DockerHostManager(DockerBaseClass):
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'),

View File

@ -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