Ansible - ansible_python_interpreter fact

Ansible uses version 2.x.x of Python to perform a number of different tasks and expects the Python command line tool to be located at /usr/bin/python. If a system has both version 2.x.x (e.g. /usr/bin/python) and version 3.x.x of Python (e.g. /usr/bin/python3), the ansible_python_interpreter variable can be used to tell Ansible to use /usr/bin/python or /usr/bin/python3.

There are many different ways to define a variable - refer to Getting Started with Variables.

The -e or --extra-vars command line option can be used, with both the ad-hoc ansible command

ansible localhost -m ping --extra-vars 'ansible_python_interpreter=/usr/bin/python3'


And with the ansible-playbook command.

ansible-playbook foo.yml --extra-vars 'ansible_python_interpreter=/usr/bin/python3'


Or the vars plugin could be used to define the ansible_python_interpreter variable.

NOTE - The gather_facts module must be set to true, which is the default setting of the gather_facts module. In other words, the gather_facts module must not be set to false.

- hosts: all
    ansible_python_interpreter: /usr/bin/python
    - name: Return Python interpreter
        msg: "Python interpreter = {{ ansible_python_interpreter }}"


Running this playbook should return the following.

TASK [debug]
ok: [] => {
    "msg: "Python interpreter = /usr/bin/python"


Add a Comment

We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.

Please enter cc44c in the box below so that we can be sure you are a human.


Web design by yours truely - me, myself, and I   |   |