1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

WIP: Check that union Jinja filter can be chained (#46298)

* Check that union Jinja filter can be chained

* set filters: fix unexpected templating type error

this error occurs with Jinja 2.10 since 32ec69d827,
for example when union filters are chained:

$ ansible all -i localhost, -mdebug -a"msg={{ []|union([])|union([]) }}"
localhost | FAILED! => {
    "msg": "Unexpected templating type error occurred on ({{ []|union([])|union([]) }}):
            unsupported operand type(s) for +: 'set' and 'list'"
}
This commit is contained in:
Pilou 2018-10-01 22:30:24 +02:00 committed by ansibot
parent d388e09940
commit b76c4c840e
2 changed files with 22 additions and 1 deletions

View file

@ -54,7 +54,12 @@ def unique(environment, a, case_sensitive=False, attribute=None):
error = None error = None
try: try:
if HAS_UNIQUE: if HAS_UNIQUE:
c = set(do_unique(environment, a, case_sensitive=case_sensitive, attribute=attribute)) c = do_unique(environment, a, case_sensitive=case_sensitive, attribute=attribute)
if isinstance(a, collections.Hashable):
c = set(c)
else:
c = list(c)
except Exception as e: except Exception as e:
if case_sensitive or attribute: if case_sensitive or attribute:
raise AnsibleFilterError("Jinja2's unique filter failed and we cannot fall back to Ansible's version " raise AnsibleFilterError("Jinja2's unique filter failed and we cannot fall back to Ansible's version "

View file

@ -237,3 +237,19 @@
that: that:
- "'00:00:00' | random_mac is match('^00:00:00:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]$')" - "'00:00:00' | random_mac is match('^00:00:00:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]:[a-f0-9][a-f0-9]$')"
- "'00:00:00' | random_mac != '00:00:00' | random_mac" - "'00:00:00' | random_mac != '00:00:00' | random_mac"
- name: Verify that union can be chained
vars:
unions: '{{ [1,2,3]|union([4,5])|union([6,7]) }}'
assert:
that:
- "unions|type_debug == 'list'"
- "unions|length == 7"
- name: Test union with unhashable item
vars:
unions: '{{ [1,2,3]|union([{}]) }}'
assert:
that:
- "unions|type_debug == 'list'"
- "unions|length == 4"