Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuring ansible to use python3 on remote targets

Tags:

ansible

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?

like image 237
C. Dodds Avatar asked Jan 21 '20 11:01

C. Dodds


People also ask

Does Ansible work with python3?

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.

Do Ansible targets require python?

By default Ansible modules require python to be present in the target machines, since they are all written in python.

Does Ansible need python on remote host?

Any ansible operation requires python on the target node except the raw and script modules.

Does Ansible use python 2 or 3?

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.

Is it possible to install Python on target host using Ansible?

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.

How do I add a Python 3 interpreter to 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.

What version of Python does Ansible run on?

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:

How do I run /usr/bin/Ansible under Python 3?

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:


1 Answers

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.

like image 54
larsks Avatar answered Sep 27 '22 23:09

larsks