Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible python version won't change

Tags:

python

ansible

I'm executing:

ansible-playbook --version

And I get the following output:

ansible-playbook 2.5.14
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'~/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]

But when I execute:

ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3'

I'm having exactly the same output. I was expecting having something like:

python version = 3.5.2

What am I misunderstanding?

like image 955
Zumo de Vidrio Avatar asked Sep 11 '25 20:09

Zumo de Vidrio


1 Answers

To change the python interpreter used on a managed host you can either use an extra or inventory variable, as per your question. This does not change the version of python used to execute ansible locally - which is mentioned in the other answer.

To confirm that you are using a different interpreter on the managed host, you will need more verbose output (at least 3 'v's, -vvv). Running python --version in a shell command is just going to show you what the system's default python version is.

Example

Python2 interpreter:

$ ansible localhost -a 'python --version' -e 'ansible_python_interpreter=/usr/bin/python2' -vvv
ansible 2.8.1
<snip>
  python version = 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)]
<snip>
'/bin/sh -c '"'"'/usr/bin/python2 /home/mattp/.ansible/tmp/ansible-tmp-1564023345.0848873-106174541151316/AnsiballZ_command.py && sleep 0'"'"''
<snip>
localhost | CHANGED | rc=0 >>
Python 2.7.16

Python3 interpreter:

$ ansible localhost -a 'python --version' -e 'ansible_python_interpreter=/usr/bin/python3' -vvv
ansible 2.8.1
<snip>
  python version = 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)]
<snip>
'/bin/sh -c '"'"'/usr/bin/python3 /home/mattp/.ansible/tmp/ansible-tmp-1564023350.3869421-223113472194736/AnsiballZ_command.py && sleep 0'"'"''
<snip>
localhost | CHANGED | rc=0 >>
Python 2.7.16

Key takeaways

  • The python version used to run Ansible locally remains unchanged
  • The system default python version remains unchanged
  • The python version used to execute the Ansible command module remotely (i.e. the python interpreter) has changed from version 2 to 3
like image 156
Matt P Avatar answered Sep 14 '25 10:09

Matt P