Bootstrap FreeKB - Ansible - Setting Python version using ansible_python_interpreter
Ansible - Setting Python version using ansible_python_interpreter

Updated:   |  Ansible articles

Often, a Linux system will contain Python version 2.x.x.

~]# python --version
Python 2.7.5

 

And Python version 3.x.x.

~]# python3 --version
Python 3.6.8

 

There are a few different ways to determine what version of Python Ansible is using and to configure Ansible to use a certain version of Python.

 

First and foremost, you almost always want to first determine the version of Python being used by Ansible. 

---
- hosts: localhost
  tasks:
  - debug:
      var: ansible_python_interpreter
...

 

Running this playbook should return something like this.

ok: [localhost] => {
    "ansible_python_interpreter": "/usr/bin/python2"
}

 

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. For example, 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.

---
- name: main play
  hosts: localhost
  vars:
    ansible_python_interpreter: /usr/bin/python
  tasks:
  - debug:
      var: ansible_python_interpreter
...

 

Running this playbook should return the following.

ok: [localhost] => {
    "ansible_python_interpreter": "/usr/bin/python2"
}

 

To be absolutely certain, you could use the Python interpreter to print the Python version.

---
- name: main play
  hosts: localhost
  vars:
    ansible_python_interpreter: /usr/bin/python3
  tasks:
  - name: test python command
    shell: "{{ ansible_python_interpreter }} --version"
    register: out

  - debug:
      var: out
...

 

Which should return something like this.

ok: [localhost] => {
    "pv": {
        "changed": true, 
        "cmd": "/usr/bin/python3 --version", 
        "delta": "0:00:00.005159", 
        "end": "2023-02-28 00:27:09.509417", 
        "failed": false, 
        "rc": 0, 
        "start": "2023-02-28 00:27:09.504258", 
        "stderr": "", 
        "stderr_lines": [], 
        "stdout": "Python 3.6.8", 
        "stdout_lines": [
            "Python 3.6.8"
        ]
    }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


November 03 2022 by Trainin99
Thanks a billion, exactly what i was looking for.

November 05 2022 by Jeremy Canfield
Awesome, glad it was helpful!

Add a Comment


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