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

**URL:** <https://forum.ansible.com/t/configuring-ansible-to-prioritize-a-specific-python-interpreter-or-automatically-discover-one/37942>\
**Category:** Ansible Project\
**Created:** [April 29, 2023, 7:56am UTC](https://forum.ansible.com/t/configuring-ansible-to-prioritize-a-specific-python-interpreter-or-automatically-discover-one/37942 "2023-04-29T07:56:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Haji\_Hoare](https://avatars.discourse-cdn.com/v4/letter/h/a587f6/32.png) [@Haji\_Hoare](https://forum.ansible.com/u/Haji_Hoare)\
**Post date:** [April 29, 2023, 7:56am UTC](https://forum.ansible.com/t/configuring-ansible-to-prioritize-a-specific-python-interpreter-or-automatically-discover-one/37942/1 "2023-04-29T07:56:33Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [April 29, 2023, 10:00am UTC](https://forum.ansible.com/t/configuring-ansible-to-prioritize-a-specific-python-interpreter-or-automatically-discover-one/37942/2 "2023-04-29T10:00:33Z")

</div>

See the configuration parameter INTERPRETER\_PYTHON\_FALLBACK  
[https://docs.ansible.com/ansible/latest/reference\_appendices/config.html#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](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).

---

<div class="post-metadata">

**Author:** ![Haji\_Hoare](https://avatars.discourse-cdn.com/v4/letter/h/a587f6/32.png) [@Haji\_Hoare](https://forum.ansible.com/u/Haji_Hoare)\
**Post date:** [April 29, 2023, 1:38pm UTC](https://forum.ansible.com/t/configuring-ansible-to-prioritize-a-specific-python-interpreter-or-automatically-discover-one/37942/3 "2023-04-29T13:38:21Z")

</div>

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:
