From 414440e3237186bf6eb2f3cb1d1fbb1ebb4186e5 Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Fri, 8 Mar 2019 14:36:53 -0600 Subject: [PATCH] Allow dict2items to work with hostvars (#53538) --- changelogs/fragments/dict2items-mapping.yaml | 2 ++ lib/ansible/plugins/filter/core.py | 4 ++-- test/integration/targets/filters/tasks/main.yml | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/dict2items-mapping.yaml diff --git a/changelogs/fragments/dict2items-mapping.yaml b/changelogs/fragments/dict2items-mapping.yaml new file mode 100644 index 0000000000..814576f013 --- /dev/null +++ b/changelogs/fragments/dict2items-mapping.yaml @@ -0,0 +1,2 @@ +bugfixes: +- dict2items - Allow dict2items to work with hostvars diff --git a/lib/ansible/plugins/filter/core.py b/lib/ansible/plugins/filter/core.py index ac66dcdd97..e7954ee887 100644 --- a/lib/ansible/plugins/filter/core.py +++ b/lib/ansible/plugins/filter/core.py @@ -45,7 +45,7 @@ from ansible.module_utils.six import iteritems, string_types, integer_types, rer from ansible.module_utils.six.moves import reduce, shlex_quote from ansible.module_utils._text import to_bytes, to_native, to_text from ansible.module_utils.common.collections import is_sequence -from ansible.module_utils.common._collections_compat import MutableMapping +from ansible.module_utils.common._collections_compat import Mapping, MutableMapping from ansible.parsing.ajson import AnsibleJSONEncoder from ansible.parsing.yaml.dumper import AnsibleDumper from ansible.utils.display import Display @@ -510,7 +510,7 @@ def dict_to_list_of_dict_key_value_elements(mydict, key_name='key', value_name=' ''' takes a dictionary and transforms it into a list of dictionaries, with each having a 'key' and 'value' keys that correspond to the keys and values of the original ''' - if not isinstance(mydict, MutableMapping): + if not isinstance(mydict, Mapping): raise AnsibleFilterError("dict2items requires a dictionary, got %s instead." % type(mydict)) ret = [] diff --git a/test/integration/targets/filters/tasks/main.yml b/test/integration/targets/filters/tasks/main.yml index 99aa9beeb3..a16698a465 100644 --- a/test/integration/targets/filters/tasks/main.yml +++ b/test/integration/targets/filters/tasks/main.yml @@ -266,3 +266,10 @@ - "'192.168.0.1/24' | ipaddr('network') == '192.168.0.0'" - "'fe80::dead:beef/64' | ipaddr('broadcast') == 'fe80::ffff:ffff:ffff:ffff'" - "'::1/120' | ipaddr('netmask') == 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00'" + +- name: Ensure dict2items works with hostvars + debug: + msg: "{{ item.key }}" + loop: "{{ hostvars|dict2items }}" + loop_control: + label: "{{ item.key }}"