Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docker_host_info: allow to list all containers #538

Merged
merged 2 commits into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/538-docker_host_info-all-containers.yml
Original file line number Diff line number Diff line change
@@ -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)."
10 changes: 9 additions & 1 deletion plugins/modules/docker_host_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"'
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand Down