Roger
Roger

Reputation: 365

Problems changing Ansible_Python_Interpreter

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#

Upvotes: 3

Views: 4816

Answers (2)

Vladimir Botka
Vladimir Botka

Reputation: 68329

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]

Upvotes: 1

Zeitounator
Zeitounator

Reputation: 44808

ansible_python_interpreter controls the version of python that is used on the target machine.

As an example on my ubuntu 18.04 machine (localhost used as target), python3 is used by default but I can switch to python 2.7:

$ ansible localhost -m setup -a filter=ansible_python_version
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_python_version": "3.6.9"
    },
    "changed": false
}
$ ansible localhost -m setup -e ansible_python_interpreter=/usr/bin/python -a filter=ansible_python_version
localhost | SUCCESS => {
    "ansible_facts": {
        "ansible_python_version": "2.7.17"
    },
    "changed": false
}

Now your example above is showing the version of python used by ansible on the controller. If you want to change that version, you have to reinstall ansible inside the specific version of python you want to use. This actually depends on how you installed ansible (rpm, deb, pip, from source...). Very basically, to go from python 2.7 to python 3.x when installed with pip:

pip uninstall ansible
pip3 install ansible

Upvotes: 2

Related Questions