Debian Codegraph @

test/integration/targets/lookup_dict/tasks/main.yml

raw
1 - name: Define users dict
2 set_fact:
3 users:
4 alice:
5 name: Alice
6 age: 21
7 bob:
8 name: Bob
9 age: 22
10
11 - name: Convert users dict to list
12 set_fact:
13 user_list: "{{ lookup('dict', users) | sort(attribute='key') }}"
14
15 - name: Verify results
16 assert:
17 that:
18 - user_list | length == 2
19 - user_list[0].key == 'alice'
20 - user_list[0].value | length == 2
21 - user_list[0].value.name == 'Alice'
22 - user_list[0].value.age == 21
23 - user_list[1].key == 'bob'
24 - user_list[1].value | length == 2
25 - user_list[1].value.name == 'Bob'
26 - user_list[1].value.age == 22
27
28 - name: Convert a non-dict (failure expected)
29 set_fact:
30 bad_fact: "{{ bbbbad }}"
31 vars:
32 bbbbad: "{{ lookup('dict', 1) }}"
33 register: result
34 ignore_errors: yes
35
36 - name: Verify conversion failed
37 assert:
38 that:
39 - result is failed
40
41 - name: Define simple dict
42 set_fact:
43 simple:
44 hello: World
45
46 - name: Convert using with_dict to cause terms to not be a list
47 set_fact:
48 hello: "{{ item }}"
49 with_dict: "{{ simple }}"
50
51 - name: Verify conversion
52 assert:
53 that:
54 - hello | length == 2
55 - hello.key == 'hello'
56 - hello.value == 'World'