Add groupby_as_dict filter (#2323)

* Add groupby_as_dict filter.

* Test all error cases.
This commit is contained in:
Felix Fontein 2021-05-13 21:49:57 +02:00 committed by GitHub
parent 23dda56913
commit 384655e15c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 123 additions and 0 deletions

View file

@ -0,0 +1,2 @@
shippable/posix/group4
skip/python2.6 # filters are controller only, and we no longer support Python 2.6 on the controller

View file

@ -0,0 +1,45 @@
---
- name: Test functionality
assert:
that:
- list1 | community.general.groupby_as_dict('name') == dict1
- name: 'Test error: not a list'
set_fact:
test: "{{ list_no_list | community.general.groupby_as_dict('name') }}"
ignore_errors: true
register: result
- assert:
that:
- result.msg == 'Input is not a sequence'
- name: 'Test error: list element not a mapping'
set_fact:
test: "{{ list_no_dict | community.general.groupby_as_dict('name') }}"
ignore_errors: true
register: result
- assert:
that:
- "result.msg == 'Sequence element #0 is not a mapping'"
- name: 'Test error: list element does not have attribute'
set_fact:
test: "{{ list_no_attribute | community.general.groupby_as_dict('name') }}"
ignore_errors: true
register: result
- assert:
that:
- "result.msg == 'Attribute not contained in element #1 of sequence'"
- name: 'Test error: attribute collision'
set_fact:
test: "{{ list_collision | community.general.groupby_as_dict('name') }}"
ignore_errors: true
register: result
- assert:
that:
- result.msg == "Multiple sequence entries have attribute value 'a'"

View file

@ -0,0 +1,31 @@
---
list1:
- name: a
x: y
- name: b
z: 1
dict1:
a:
name: a
x: y
b:
name: b
z: 1
list_no_list:
a:
name: a
list_no_dict:
- []
- 1
list_no_attribute:
- name: a
foo: baz
- foo: bar
list_collision:
- name: a
- name: a