diff --git a/changelogs/fragments/160-docker_host_info-label-fitler-lists.yml b/changelogs/fragments/160-docker_host_info-label-fitler-lists.yml new file mode 100644 index 000000000..4b58bfc42 --- /dev/null +++ b/changelogs/fragments/160-docker_host_info-label-fitler-lists.yml @@ -0,0 +1,4 @@ +--- +minor_changes: + - docker_host_info - allow values for keys in ``containers_filters``, ``images_filters``, ``networks_filters``, and + ``volumes_filters`` to be passed as YAML lists (https://github.com/ansible-collections/community.docker/pull/160). diff --git a/plugins/module_utils/common.py b/plugins/module_utils/common.py index fbe2ba28c..5c3399a68 100644 --- a/plugins/module_utils/common.py +++ b/plugins/module_utils/common.py @@ -15,6 +15,7 @@ from ansible.module_utils.basic import AnsibleModule, env_fallback, missing_required_lib +from ansible.module_utils.common.collections import is_sequence from ansible.module_utils.common._collections_compat import Mapping, Sequence from ansible.module_utils.six import string_types from ansible.module_utils.six.moves.urllib.parse import urlparse @@ -921,23 +922,27 @@ def get_legacy_docker_diffs(self): return result -def clean_dict_booleans_for_docker_api(data): +def clean_dict_booleans_for_docker_api(data, allow_sequences=False): ''' Go doesn't like Python booleans 'True' or 'False', while Ansible is just fine with them in YAML. As such, they need to be converted in cases where we pass dictionaries to the Docker API (e.g. docker_network's - driver_options and docker_prune's filters). + driver_options and docker_prune's filters). When `allow_sequences=True` + YAML sequences (lists, tuples) are converted to [str] instead of str([...]) + which is the expected format of filters which accept lists such as labels. ''' + def sanitize(value): + if value is True: + return 'true' + elif value is False: + return 'false' + else: + return str(value) + result = dict() if data is not None: for k, v in data.items(): - if v is True: - v = 'true' - elif v is False: - v = 'false' - else: - v = str(v) - result[str(k)] = v + result[str(k)] = [sanitize(e) for e in v] if allow_sequences and is_sequence(v) else sanitize(v) return result diff --git a/plugins/modules/docker_host_info.py b/plugins/modules/docker_host_info.py index 3700a32a8..599a92d75 100644 --- a/plugins/modules/docker_host_info.py +++ b/plugins/modules/docker_host_info.py @@ -33,6 +33,8 @@ description: - A dictionary of filter values used for selecting containers to list. - "For example, C(until: 24h)." + - C(label) is a special case of filter which can be a string C() matching when a label is present, a string + C(=) matching when a label has a particular value, or a list of strings C()/C(=) matching when a label is present, a string + C(=) matching when a label has a particular value, or a list of strings C()/C(=) matching when a label is present, a string + C(=) matching when a label has a particular value, or a list of strings C()/C(=) matching when a label is present, a string + C(=) matching when a label has a particular value, or a list of strings C()/C(=