Problems changing Ansible_Python_Interpreter

Tags: , ,



How can I change my ansible_python_interpreter value on Ubuntu?

I downloaded and installed Python 2.7.12 from tar, and now it’s running by default outside of Ansible

# which python
/usr/local/bin/python
#python --version
Python 2.7.12

But when I try to set the variable, Ansible shows that it’s still using that newer version of Python (I need to use this older version to test)

# ansible-playbook --version -e "ansible_python_interpreter=/usr/local/bin/python"

ansible-playbook 2.5.1
  config file = /home/fortinet/Downloads/ansible/playbooks/complete_provisioning/ansible.cfg
  configured module search path = [u'/home/fortinet/Downloads/ansible/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.15rc1 (default, Nov 12 2018, 14:31:15) [GCC 7.3.0]
root@ubuntu18:/home/fortinet/Downloads/ansible/playbooks/complete_provisioning#

Answer

It’s not possible to configure the version of Python used by Ansible on the controller.

ANSIBLE_PYTHON_INTERPRETER configuration parameter will set:

Path to the Python interpreter to be used for module execution on remote targets

The version of Python on controller depends on how Ansible has been built. For example

shell> grep DISTRIB_DESCRIPTION /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"

shell> dpkg -l | grep ansible
ii  ansible                                2.9.6-1ppa~bionic

shell> ansible --version
ansible 2.9.6
  config file = /home/admin/.ansible.cfg
  configured module search path = [u'/home/admin/.ansible/my_modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Nov  7 2019, 10:07:09) [GCC 7.4.0]
shell> grep DISTRIB_DESCRIPTION /etc/lsb-release
DISTRIB_DESCRIPTION="Ubuntu 20.04 LTS"

shell> dpkg -l | grep ansible
ii  ansible                               2.9.6+dfsg-1

shell> ansible --version
ansible 2.9.6
  config file = /home/admin/.ansible.cfg
  configured module search path = ['/home/admin/.ansible/my_modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0]


Source: stackoverflow