summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/interpreter_discovery_python/tasks/main.yml
blob: 7e9b2e87a589807bc8e8026522a7f2c7055066c2 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
- name: ensure we can override ansible_python_interpreter
  vars:
    ansible_python_interpreter: overriddenpython
  assert:
    that:
    - ansible_python_interpreter == 'overriddenpython'
    fail_msg: "'ansible_python_interpreter' appears to be set at a high precedence to {{ ansible_python_interpreter }},
              which breaks this test."

- name: snag some facts to validate for later
  set_fact:
    distro: '{{ ansible_distribution | default("unknown") | lower }}'
    distro_version: '{{ ansible_distribution_version | default("unknown") }}'
    distro_major_version: '{{ ansible_distribution_major_version | default("unknown") }}'
    os_family: '{{ ansible_os_family | default("unknown") }}'

- name: test that python discovery is working and that fact persistence makes it only run once
  block:
  - name: clear facts to force interpreter discovery to run
    meta: clear_facts

  - name: trigger discovery with auto
    vars:
      ansible_python_interpreter: auto
    ping:
    register: auto_out

  - name: get the interpreter being used on the target to execute modules
    vars:
      # keep this set so we can verify we didn't repeat discovery
      ansible_python_interpreter: auto
    test_echo_module:
    register: echoout

  - name: clear facts to force interpreter discovery to run again
    meta: clear_facts

  - name: get the interpreter being used on the target to execute modules with ansible_facts
    vars:
      # keep this set so we can verify we didn't repeat discovery
      ansible_python_interpreter: auto
    test_echo_module:
      facts:
        sandwich: ham
    register: echoout_with_facts

  - when: distro == 'macosx'
    block:
      - name: Get the sys.executable for the macos discovered interpreter, as it may be different than the actual path
        raw: '{{ auto_out.ansible_facts.discovered_interpreter_python }} -c "import sys; print(sys.executable)"'
        register: discovered_sys_executable

      - set_fact:
          normalized_discovered_interpreter: '{{ discovered_sys_executable.stdout_lines[0] }}'

  - set_fact:
      normalized_discovered_interpreter: '{{ auto_out.ansible_facts.discovered_interpreter_python }}'
    when: distro != 'macosx'

  - assert:
      that:
      - auto_out.ansible_facts.discovered_interpreter_python is defined
      - echoout.running_python_interpreter == normalized_discovered_interpreter
      # verify that discovery didn't run again (if it did, we'd have the fact in the result)
      - echoout.ansible_facts is not defined or echoout.ansible_facts.discovered_interpreter_python is not defined
      - echoout_with_facts.ansible_facts is defined
      - echoout_with_facts.running_python_interpreter == normalized_discovered_interpreter

- name: test that auto_legacy gives a dep warning when /usr/bin/python present but != auto result
  block:
  - name: clear facts to force interpreter discovery to run
    meta: clear_facts

  - name: trigger discovery with auto_legacy
    vars:
      ansible_python_interpreter: auto_legacy
    ping:
    register: legacy

  - name: check for warning (only on platforms where auto result is not /usr/bin/python and legacy is)
    assert:
      that:
      - legacy.warnings | default([]) | length > 0
    # only check for a dep warning if legacy returned /usr/bin/python and auto didn't
    when: legacy.ansible_facts.discovered_interpreter_python == '/usr/bin/python' and
          auto_out.ansible_facts.discovered_interpreter_python != '/usr/bin/python'


- name: test that auto_silent never warns and got the same answer as auto
  block:
  - name: clear facts to force interpreter discovery to run
    meta: clear_facts

  - name: initial task to trigger discovery
    vars:
      ansible_python_interpreter: auto_silent
    ping:
    register: auto_silent_out

  - assert:
      that:
      - auto_silent_out.warnings is not defined
      - auto_silent_out.ansible_facts.discovered_interpreter_python == auto_out.ansible_facts.discovered_interpreter_python


- name: test that auto_legacy_silent never warns and got the same answer as auto_legacy
  block:
  - name: clear facts to force interpreter discovery to run
    meta: clear_facts

  - name: trigger discovery with auto_legacy_silent
    vars:
      ansible_python_interpreter: auto_legacy_silent
    ping:
    register: legacy_silent

  - assert:
      that:
        - legacy_silent.warnings is not defined
        - legacy_silent.ansible_facts.discovered_interpreter_python == legacy.ansible_facts.discovered_interpreter_python

- name: ensure modules can't set discovered_interpreter_X or ansible_X_interpreter
  block:
  - test_echo_module:
      facts:
        ansible_discovered_interpreter_bogus: from module
        discovered_interpreter_bogus: from_module
        ansible_bogus_interpreter: from_module
        test_fact: from_module
    register: echoout

  - assert:
      that:
      - test_fact == 'from_module'
      - discovered_interpreter_bogus | default('nope') == 'nope'
      - ansible_bogus_interpreter | default('nope') == 'nope'
      # this one will exist in facts, but with its prefix removed
      - ansible_facts['ansible_bogus_interpreter'] | default('nope') == 'nope'
      - ansible_facts['discovered_interpreter_bogus'] | default('nope') == 'nope'

  - name: debian assertions
    assert:
      that:
      # Debian 8 and older
      - auto_out.ansible_facts.discovered_interpreter_python == '/usr/bin/python' and distro_version is version('8', '<=') or distro_version is version('8', '>')
      # Debian 10 and newer
      - auto_out.ansible_facts.discovered_interpreter_python == '/usr/bin/python3' and distro_version is version('10', '>=') or distro_version is version('10', '<')
    when: distro == 'debian'

  - name: fedora assertions
    assert:
      that:
      - auto_out.ansible_facts.discovered_interpreter_python == '/usr/bin/python3'
    when: distro == 'fedora' and distro_version is version('23', '>=')

  - name: rhel assertions
    assert:
      that:
      # rhel 6/7
      - (auto_out.ansible_facts.discovered_interpreter_python == '/usr/bin/python' and distro_major_version is version('8','<')) or distro_major_version is version('8','>=')
      # rhel 8
      - (auto_out.ansible_facts.discovered_interpreter_python == '/usr/libexec/platform-python' and distro_major_version is version('8','==')) or distro_major_version is version('8','!=')
      # rhel 9
      - (auto_out.ansible_facts.discovered_interpreter_python == '/usr/bin/python3' and distro_major_version is version('9','==')) or distro_major_version is version('9','!=')
    when: distro == 'redhat'

  - name: ubuntu assertions
    assert:
      that:
      # ubuntu < 16
      - (auto_out.ansible_facts.discovered_interpreter_python == '/usr/bin/python' and distro_version is version('16.04','<')) or distro_version is version('16.04','>=')
      # ubuntu >= 16
      - (auto_out.ansible_facts.discovered_interpreter_python == '/usr/bin/python3' and distro_version is version('16.04','>=')) or distro_version is version('16.04','<')
    when: distro == 'ubuntu'

  - name: mac assertions
    assert:
      that:
      - auto_out.ansible_facts.discovered_interpreter_python == '/usr/bin/python'
    when: os_family == 'darwin'

  always:
  - meta: clear_facts