summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/lookup_dict/tasks/main.yml
blob: b1324136148eeb931fdc75ede669c23b14b85ce2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
- name: Define users dict
  set_fact:
    users:
      alice:
        name: Alice
        age: 21
      bob:
        name: Bob
        age: 22

- name: Convert users dict to list
  set_fact:
    user_list: "{{ lookup('dict', users) | sort(attribute='key') }}"

- name: Verify results
  assert:
    that:
    - user_list | length == 2
    - user_list[0].key == 'alice'
    - user_list[0].value | length == 2
    - user_list[0].value.name == 'Alice'
    - user_list[0].value.age == 21
    - user_list[1].key == 'bob'
    - user_list[1].value | length == 2
    - user_list[1].value.name == 'Bob'
    - user_list[1].value.age == 22

- name: Convert a non-dict (failure expected)
  set_fact:
    bad_fact: "{{ bbbbad }}"
  vars:
    bbbbad: "{{ lookup('dict', 1) }}"
  register: result
  ignore_errors: yes

- name: Verify conversion failed
  assert:
    that:
      - result is failed

- name: Define simple dict
  set_fact:
    simple:
      hello: World

- name: Convert using with_dict to cause terms to not be a list
  set_fact:
    hello: "{{ item }}"
  with_dict: "{{ simple }}"

- name: Verify conversion
  assert:
    that:
      - hello | length == 2
      - hello.key == 'hello'
      - hello.value == 'World'