summaryrefslogtreecommitdiff
path: root/test/integration/targets/lookup_first_found/tasks/main.yml
blob: ba248bd5ddb6f5133f506e4c98183cd8f1462213 (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
- name: test with_first_found
  set_fact: "first_found={{ item }}"
  with_first_found:
    - "does_not_exist"
    - "foo1"
    - "{{ role_path + '/files/bar1' }}"   # will only hit this if dwim search is broken

- name: set expected
  set_fact: first_expected="{{ role_path + '/files/foo1' }}"

- name: set unexpected
  set_fact: first_unexpected="{{ role_path + '/files/bar1' }}"

- name: verify with_first_found results
  assert:
    that:
        - "first_found == first_expected"
        - "first_found != first_unexpected"

- name: test q(first_found) with no files produces empty list
  set_fact:
    first_found_var: "{{ q('first_found', params, errors='ignore') }}"
  vars:
    params:
      files: "not_a_file.yaml"
      skip: True

- name: verify q(first_found) result
  assert:
    that:
      - "first_found_var == []"

- name: test lookup(first_found) with no files produces empty string
  set_fact:
    first_found_var: "{{ lookup('first_found', params, errors='ignore') }}"
  vars:
    params:
      files: "not_a_file.yaml"

- name: verify lookup(first_found) result
  assert:
    that:
      - "first_found_var == ''"

# NOTE: skip: True deprecated e17a2b502d6601be53c60d7ba1c627df419460c9, remove 2.12
- name: test first_found with no matches and skip=True does nothing
  set_fact: "this_not_set={{ item }}"
  vars:
    params:
      files:
        - not/a/file.yaml
        - another/non/file.yaml
      skip: True
  loop: "{{ q('first_found', params) }}"

- name: verify skip
  assert:
    that:
      - "this_not_set is not defined"

- name: test first_found with no matches and errors='ignore' skips in a loop
  set_fact: "this_not_set={{ item }}"
  vars:
    params:
      files:
        - not/a/file.yaml
        - another/non/file.yaml
  loop: "{{ query('first_found', params, errors='ignore') }}"

- name: verify errors=ignore
  assert:
    that:
      - "this_not_set is not defined"

- name: test legacy formats
  set_fact: hatethisformat={{item}}
  vars:
      params:
        files: not/a/file.yaml;hosts
        paths: not/a/path:/etc
  loop: "{{ q('first_found', params) }}"

- name: verify /etc/hosts was found
  assert:
    that:
      - "hatethisformat == '/etc/hosts'"

- name: test spaces in names
  include_vars: "{{ item }}"
  with_first_found:
    - files:
      - "{{ role_path + '/files/vars file spaces.yml' }}"

- assert:
    that:
      - foo is defined

# TODO: no 'terms' test
- name: test first_found lookup with no terms
  set_fact:
    no_terms: "{{ query('first_found', files=['missing1', 'hosts', 'missing2'], paths=['/etc'], errors='ignore') }}"

- assert:
    that: "no_terms|first == '/etc/hosts'"

- name: handle templatable dictionary entries
  block:

  - name: Load variables specific for OS family
    assert:
      that:
        - "item is file"
        - "item|basename == 'itworks.yml'"
    with_first_found:
      - files:
          - "{{ansible_id}}-{{ansible_lsb.major_release}}.yml"  # invalid var, should be skipped
          - "{{ansible_lsb.id}}-{{ansible_lsb.major_release}}.yml"  # does not exist, but should try
          - "{{ansible_distribution}}-{{ansible_distribution_major_version}}.yml"  # does not exist, but should try
          - itworks.yml
          - ishouldnotbefound.yml  # this exist, but should not be found
        paths:
          - "{{role_path}}/vars"

  - name: Load variables specific for OS family, but now as list of dicts, same options as above
    assert:
      that:
        - "item is file"
        - "item|basename == 'itworks.yml'"
    with_first_found:
      - files:
          - "{{ansible_id}}-{{ansible_lsb.major_release}}.yml"
        paths:
          - "{{role_path}}/vars"
      - files:
          - "{{ansible_lsb.id}}-{{ansible_lsb.major_release}}.yml"
        paths:
          - "{{role_path}}/vars"
      - files:
          - "{{ansible_distribution}}-{{ansible_distribution_major_version}}.yml"
        paths:
          - "{{role_path}}/vars"
      - files:
          - itworks.yml
        paths:
          - "{{role_path}}/vars"
      - files:
          - ishouldnotbefound.yml
        paths:
          - "{{role_path}}/vars"