summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/lookup_vars/tasks/main.yml
blob: 57b05b8f2326e67caf4b58fffa2a7748627dce76 (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: Test that we can give it a single value and receive a single value
  set_fact:
    var_host: '{{ lookup("vars", "ansible_host") }}'

- assert:
    that:
      - 'var_host == ansible_host'

- name: Test that we can give a list of values to var and receive a list of values back
  set_fact:
    var_host_info: '{{ query("vars", "ansible_host", "ansible_connection") }}'

- assert:
    that:
      - 'var_host_info[0] == ansible_host'
      - 'var_host_info[1] == ansible_connection'

- block:
  - name: EXPECTED FAILURE - test invalid var
    debug:
      var: '{{ lookup("vars", "doesnotexist") }}'

  - fail:
      msg: "should not get here"

  rescue:
    - assert:
        that:
          - ansible_failed_task.name == "EXPECTED FAILURE - test invalid var"
          - expected in ansible_failed_result.msg
      vars:
        expected: "No variable found with this name: doesnotexist"

- block:
  - name: EXPECTED FAILURE - test invalid var type
    debug:
      var: '{{ lookup("vars", 42) }}'

  - fail:
      msg: "should not get here"

  rescue:
    - assert:
        that:
          - ansible_failed_task.name == "EXPECTED FAILURE - test invalid var type"
          - expected in ansible_failed_result.msg
      vars:
        expected: "Invalid setting identifier, \"42\" is not a string"

- name: test default
  set_fact:
    expected_default_var: '{{ lookup("vars", "doesnotexist", default="some text") }}'

- assert:
    that:
      - expected_default_var == "some text"