| 1 |
- name: ensure we can override ansible_python_interpreter |
| 2 |
vars: |
| 3 |
ansible_python_interpreter: overriddenpython |
| 4 |
assert: |
| 5 |
that: |
| 6 |
- ansible_python_interpreter == 'overriddenpython' |
| 7 |
fail_msg: "'ansible_python_interpreter' appears to be set at a high precedence to {{ ansible_python_interpreter }}, |
| 8 |
which breaks this test." |
| 9 |
|
| 10 |
- name: snag some facts to validate for later |
| 11 |
set_fact: |
| 12 |
distro: '{{ ansible_distribution | lower }}' |
| 13 |
distro_version: '{{ ansible_distribution_version }}' |
| 14 |
distro_major_version: '{{ ansible_distribution_major_version }}' |
| 15 |
os_family: '{{ ansible_os_family }}' |
| 16 |
|
| 17 |
- name: test that python discovery is working and that fact persistence makes it only run once |
| 18 |
block: |
| 19 |
- name: clear facts to force interpreter discovery to run |
| 20 |
meta: clear_facts |
| 21 |
|
| 22 |
- name: trigger discovery with auto |
| 23 |
vars: |
| 24 |
ansible_python_interpreter: auto |
| 25 |
ping: |
| 26 |
register: auto_out |
| 27 |
|
| 28 |
- name: get the interpreter being used on the target to execute modules |
| 29 |
vars: |
| 30 |
# keep this set so we can verify we didn't repeat discovery |
| 31 |
ansible_python_interpreter: auto |
| 32 |
test_echo_module: |
| 33 |
register: echoout |
| 34 |
|
| 35 |
- name: clear facts to force interpreter discovery to run again |
| 36 |
meta: clear_facts |
| 37 |
|
| 38 |
- name: get the interpreter being used on the target to execute modules with ansible_facts |
| 39 |
vars: |
| 40 |
# keep this set so we can verify we didn't repeat discovery |
| 41 |
ansible_python_interpreter: auto |
| 42 |
test_echo_module: |
| 43 |
facts: |
| 44 |
sandwich: ham |
| 45 |
register: echoout_with_facts |
| 46 |
|
| 47 |
- when: distro == 'macosx' |
| 48 |
block: |
| 49 |
- name: Get the sys.executable for the macos discovered interpreter, as it may be different than the actual path |
| 50 |
raw: '{{ auto_out.ansible_facts.discovered_interpreter_python }} -c "import sys; print(sys.executable)"' |
| 51 |
register: discovered_sys_executable |
| 52 |
|
| 53 |
- set_fact: |
| 54 |
normalized_discovered_interpreter: '{{ discovered_sys_executable.stdout_lines[0] }}' |
| 55 |
|
| 56 |
- set_fact: |
| 57 |
normalized_discovered_interpreter: '{{ auto_out.ansible_facts.discovered_interpreter_python }}' |
| 58 |
when: distro != 'macosx' |
| 59 |
|
| 60 |
- assert: |
| 61 |
that: |
| 62 |
- auto_out.ansible_facts.discovered_interpreter_python is defined |
| 63 |
# whatever it found should be python 3.x |
| 64 |
- auto_out.ansible_facts.discovered_interpreter_python is search 'bin/python3.*' |
| 65 |
- echoout.running_python_interpreter == normalized_discovered_interpreter |
| 66 |
# verify that discovery didn't run again (if it did, we'd have the fact in the result) |
| 67 |
- echoout.ansible_facts is not defined or echoout.ansible_facts.discovered_interpreter_python is not defined |
| 68 |
- echoout_with_facts.ansible_facts is defined |
| 69 |
- echoout_with_facts.running_python_interpreter == normalized_discovered_interpreter |
| 70 |
|
| 71 |
- name: test no interpreter found behavior |
| 72 |
block: |
| 73 |
- name: clear facts to force interpreter discovery to run |
| 74 |
meta: clear_facts |
| 75 |
|
| 76 |
- name: trigger discovery with auto |
| 77 |
vars: |
| 78 |
ansible_python_interpreter: auto |
| 79 |
ansible_interpreter_python_fallback: |
| 80 |
- /usr/bin/does_not_exist |
| 81 |
ping: |
| 82 |
register: discovery |
| 83 |
ignore_errors: yes # the fallback interpreter /usr/bin/python3 may not exist (e.g. FreeBSD) |
| 84 |
|
| 85 |
- name: check for warning and default interpreter |
| 86 |
assert: |
| 87 |
that: |
| 88 |
- discovery.warnings | length == 1 |
| 89 |
- discovery.warnings[0] is contains "No python interpreters found for host" |
| 90 |
- discovery.ansible_facts.discovered_interpreter_python == '/usr/bin/python3' |
| 91 |
|
| 92 |
- name: clear facts to force interpreter discovery to run |
| 93 |
meta: clear_facts |
| 94 |
|
| 95 |
- name: trigger discovery with auto_silent |
| 96 |
vars: |
| 97 |
ansible_python_interpreter: auto_silent |
| 98 |
ansible_interpreter_python_fallback: |
| 99 |
- /usr/bin/does_not_exist |
| 100 |
ping: |
| 101 |
register: discovery |
| 102 |
ignore_errors: yes # the fallback interpreter /usr/bin/python3 may not exist (e.g. FreeBSD) |
| 103 |
|
| 104 |
- name: verify auto_silent suppresses warning |
| 105 |
assert: |
| 106 |
that: |
| 107 |
- discovery.warnings | default([]) | length == 0 |
| 108 |
- discovery.ansible_facts.discovered_interpreter_python == '/usr/bin/python3' |
| 109 |
|
| 110 |
- name: test bad fallback interpreter |
| 111 |
block: |
| 112 |
- name: clear facts to force interpreter discovery to run |
| 113 |
meta: clear_facts |
| 114 |
|
| 115 |
- name: trigger discovery with auto |
| 116 |
vars: |
| 117 |
ansible_python_interpreter: auto |
| 118 |
# This test takes advantage of the fact the existing code performs manual quoting instead of using shlex.quote. |
| 119 |
# If that is ever fixed, it may be difficult (or impossible) to trigger the error condition covered here. |
| 120 |
ansible_interpreter_python_fallback: |
| 121 |
- "'i_have_a_single_quote" |
| 122 |
ping: |
| 123 |
register: discovery |
| 124 |
ignore_errors: yes # the fallback interpreter /usr/bin/python3 may not exist (e.g. FreeBSD) |
| 125 |
|
| 126 |
- debug: |
| 127 |
var: discovery |
| 128 |
|
| 129 |
- name: check for warning and default interpreter |
| 130 |
assert: |
| 131 |
that: |
| 132 |
- discovery.warnings | length == 2 |
| 133 |
- discovery.warnings[0] is contains "Unhandled error in Python interpreter discovery for host" |
| 134 |
- discovery.warnings[1] is contains "Host 'testhost' is using the discovered Python interpreter at '/usr/bin/python3'" |
| 135 |
- discovery.ansible_facts.discovered_interpreter_python == '/usr/bin/python3' |
| 136 |
|
| 137 |
- name: clear facts to force interpreter discovery to run |
| 138 |
meta: clear_facts |
| 139 |
|
| 140 |
- name: trigger discovery with auto_silent |
| 141 |
vars: |
| 142 |
ansible_python_interpreter: auto_silent |
| 143 |
# This test takes advantage of the fact the existing code performs manual quoting instead of using shlex.quote. |
| 144 |
# If that is ever fixed, it may be difficult (or impossible) to trigger the error condition covered here. |
| 145 |
ansible_interpreter_python_fallback: |
| 146 |
- "'i_have_a_single_quote" |
| 147 |
ping: |
| 148 |
register: discovery |
| 149 |
ignore_errors: yes # the fallback interpreter /usr/bin/python3 may not exist (e.g. FreeBSD) |
| 150 |
|
| 151 |
- debug: |
| 152 |
var: discovery |
| 153 |
|
| 154 |
- name: verify auto_silent suppresses warning |
| 155 |
assert: |
| 156 |
that: |
| 157 |
- discovery.warnings | default([]) | length == 0 |
| 158 |
- discovery.ansible_facts.discovered_interpreter_python == '/usr/bin/python3' |
| 159 |
|
| 160 |
- name: test that auto_silent never warns and got the same answer as auto |
| 161 |
block: |
| 162 |
- name: clear facts to force interpreter discovery to run |
| 163 |
meta: clear_facts |
| 164 |
|
| 165 |
- name: initial task to trigger discovery |
| 166 |
vars: |
| 167 |
ansible_python_interpreter: auto_silent |
| 168 |
ping: |
| 169 |
register: auto_silent_out |
| 170 |
|
| 171 |
- assert: |
| 172 |
that: |
| 173 |
- auto_silent_out.warnings is not defined |
| 174 |
- auto_silent_out.ansible_facts.discovered_interpreter_python == auto_out.ansible_facts.discovered_interpreter_python |
| 175 |
|
| 176 |
- name: ensure modules can't set discovered_interpreter_X or ansible_X_interpreter |
| 177 |
block: |
| 178 |
- test_echo_module: |
| 179 |
facts: |
| 180 |
ansible_discovered_interpreter_bogus: from module |
| 181 |
discovered_interpreter_bogus: from_module |
| 182 |
ansible_bogus_interpreter: from_module |
| 183 |
test_fact: from_module |
| 184 |
register: echoout |
| 185 |
|
| 186 |
- assert: |
| 187 |
that: |
| 188 |
- test_fact == 'from_module' |
| 189 |
- discovered_interpreter_bogus | default('nope') == 'nope' |
| 190 |
- ansible_bogus_interpreter | default('nope') == 'nope' |
| 191 |
# this one will exist in facts, but with its prefix removed |
| 192 |
- ansible_facts['ansible_bogus_interpreter'] | default('nope') == 'nope' |
| 193 |
- ansible_facts['discovered_interpreter_bogus'] | default('nope') == 'nope' |
| 194 |
|
| 195 |
always: |
| 196 |
- meta: clear_facts |
| 197 |
|
| 198 |
- include_tasks: config_templating.yml |