I've been looking into attempting to get ansible to use python3 on remote targets, in order to run playbooks against them, however, simply running a playbook against a target with python3 installed fails with the error message:
"/bin/sh: 1: /usr/bin/python: not found\r\n"
Looking for answers to this online only seem to discuss configuring ansible on the host to use python3 rather than the remote. Is it possible to configure the remote to use python3 rather than 2?
Ansible will automatically detect and use Python 3 on many platforms that ship with it. To explicitly configure a Python 3 interpreter, set the ansible_python_interpreter inventory variable at a group or host level to the location of a Python 3 interpreter, such as /usr/bin/python3.
By default Ansible modules require python to be present in the target machines, since they are all written in python.
Any ansible operation requires python on the target node except the raw and script modules.
Currently Ansible can be run from any machine with Python 2 (version 2.7) or Python 3 (versions 3.5 and higher) installed. Windows isn't supported for the control machine. This includes Red Hat, Debian, CentOS, macOS, any of the BSDs, and so on.
So for this ansible has raw module. This module is very useful and is used in various cases, one of is installing python on target host. Let’s begin that how can we install python on target host using ansible.
To explicitly configure a Python 3 interpreter, set the ansible_python_interpreter inventory variable at a group or host level to the location of a Python 3 interpreter, such as /usr/bin/python3. The default interpreter path may also be set in ansible.cfg.
Ansible supports Python version 3.5 and above only. The easiest way to run /usr/bin/ansible under Python 3 is to install it with the Python3 version of pip. This will make the default /usr/bin/ansible run with Python3:
The easiest way to run /usr/bin/ansible under Python 3 is to install it with the Python3 version of pip. This will make the default /usr/bin/ansible run with Python3:
You can set the ansible_python_interpreter
variable to tell Ansible which version of Python to use. You can set this globally, as C. Dodds has suggested in their answer, but it generally makes more sense to set this as a per-host inventory variable. E.g., using a YAML inventory:
all:
hosts:
myhost:
ansible_python_interpreter: /usr/bin/python3
Or using an ini-style inventory:
myhost ansible_python_interpreter=/usr/bin/python3
And of course you can set this per-hostgroup if you have several hosts that require the same configuration.
This is discussed in the Ansible documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With