1

as you can see from the code snippet below, ansible's executable seems to be ignoring the ansible_python_interpreter variable. Without resorting to an inventory file (this is for a gitlab pipeline based on hashicorp's packer - which calls ansible), how could I enforce ansible to use whatever python version I have? In this case, it's an Ubuntu 18.04, so I want to switch from python-2.7 to python-3.6.

Another related cause couldto be the fact that ansibles pkg (policy?) always installs python-2.7 on bionic.

root@ubuntu18:~# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.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
  python version = 2.7.17 (default, Feb 27 2021, 15:10:58) [GCC 7.5.0]
root@ubuntu18:~# /usr/bin/python3.6 --version
Python 3.6.9
root@ubuntu18:~# ansible --version -e 'ansible_python_interpreter=/usr/bin/python3.6'
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.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
  python version = 2.7.17 (default, Feb 27 2021, 15:10:58) [GCC 7.5.0]
root@ubuntu18:~#

My last resort seems to be installing ansible using pip3, but that would require a significant rewrite of both the pipeline yaml and packer's json.

Any help would be greatly appreciated.

Cheers

rofz
  • 95
  • 8
  • If you want to set the Python interpreter for individual hosts and groups, set the ansible_python_interpreter inventory variable. – Frenchy Nov 23 '21 at 17:28
  • Right, I make the same observation in my environment, RHEL 7.9, Ansible 2.9.25, Python 2.7.5 and 3.6.8, `ansible --version -e 'ansible_python_interpreter=/usr/bin/python3.6' ansible 2.9.25 ... python version = 2.7.5 ...`. `python3 --version Python 3.6.8`. – U880D Nov 23 '21 at 17:30
  • Based on flowerysong's answer it seems to be necessary to check [Ansible: Python version won't change](https://stackoverflow.com/questions/57167091/), [Python version change in Ansible](https://stackoverflow.com/questions/59767501/) or [Ansible is using wrong version of Python](https://stackoverflow.com/questions/61257164/). – U880D Nov 23 '21 at 17:59

1 Answers1

1

You have not provided any evidence that the parameter is being ignored. ansible_python_intepreter applies to the execution of modules on the targets, not to the execution of Ansible itself.

The Ansible control process always uses the Python interpreter it was installed under; the best way to change this is to change your install process.

flowerysong
  • 2,921
  • 4
  • 13
  • Ah, "_The Ansible control process always uses the Python interpreter it was installed under;_", that explains some of the behavior I've seen. Any idea how to check under which version it was installed? – U880D Nov 23 '21 at 17:41
  • 1
    Run `ansible --version` – flowerysong Nov 23 '21 at 17:48
  • There I found the reason for some of the behavior: `ansible python module location = /usr/lib/python2.7/site-packages/ansible`. We may need to follow [Ansible: How to change python version?](https://stackoverflow.com/questions/59716485/) – U880D Nov 23 '21 at 17:50