Configuring Ansible to prioritize a specific Python interpreter or automatically discover one

Hi,

I’d like to ask for advice on configuring Ansible to prioritize using a specific Python interpreter if it exists or automatically discover the appropriate Python interpreter if the preferred one is not present, especially in cases where the preferred interpreter does not exist initially. As an example, I want to install and use /usr/bin/python3 in an environment where only /usr/bin/python exists by default.

Ideally, I would like to define this behavior in the inventory file (inventory.ini), so that Ansible would use /usr/bin/python3 if available, and fall back to automatic discovery otherwise.

How can I achieve this? Are there any recommended configurations or best practices for this scenario?

See the configuration parameter INTERPRETER_PYTHON_FALLBACK
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#interpreter-python-fallback

You can put it into the inventory file
ansible_interpreter_python_fallback=['/usr/bin/python3','/usr/bin/python']

See also the configuration parameter INTERPRETER_PYTHON
https://docs.ansible.com/ansible/latest/reference_appendices/config.html#interpreter-python

The fallback will be used if INTERPRETER_PYTHON is not available.
"The fallback behavior will issue a warning that the interpreter
should be set explicitly..." You can chose a discovery mode (default
auto).

Thanks for your help and detailed explanation on the Python interpreter selection in Ansible.

After our discussion, I confirmed that I can achieve my initial goal (prioritizing ‘/usr/bin/python3’ when both ‘/usr/bin/python3’ and ‘/usr/bin/python’ are present, and automatically detecting other Python interpreters when ‘/usr/bin/python3’ is not available) by adjusting the INTERPRETER_PYTHON setting.

Initially, I faced issues with the default behavior prioritizing ‘/usr/bin/python’ when both were present. My environment is using Ansible 2.10, which sets INTERPRETER_PYTHON to ‘auto_legacy’ by default. Based on your advice, I found that changing INTERPRETER_PYTHON to ‘auto’ or ‘auto_silent’ resolved the issue in a straightforward manner.

I appreciate your assistance in resolving this problem.

Best regards,

Haji

2023年4月29日土曜日 19:00:57 UTC+9 Vladimir Botka: