Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I specify the version of Python to use in an Ansible playbook?

Tags:

ansible

I'm working on a project that is still running Python 2. I'm trying to use Ansible to set up new test servers. The base Linux installation that I start with only has Python 3, so I need my very first "bootstrap" playbook to use Python 3, but then I want subsequent playbooks to use Python 2.

I can specify the version of python in my inventory file like this:

[test_server:vars]
ansible_python_interpreter=/usr/bin/python3

[test_server]
test_server.example.com

But then I have to go edit the inventory file to make sure that I'm using Python 3 for the bootstrap playbook, and then edit it again for the rest of my playbooks. That seems odd. I've tried a few different versions of changing ansible_python_interpreter within my playbooks, like

- hosts: test_server
    ansible_python_interpreter: /usr/bin/python

and

- hosts: test_server
  tasks:
    - name: install pip
      ansible_python_interpreter: /usr/bin/python
      apt:
        name: python-pip

but ansible complains that

ERROR! 'ansible_python_interpreter' is not a valid attribute for a Task

Even though https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html says that

You can still set ansible_python_interpreter to a specific path at any variable level (for example, in host_vars, in vars files, in playbooks, etc.).

What's the invocation to do this correctly?

like image 796
Chris Curvey Avatar asked Oct 18 '19 12:10

Chris Curvey


People also ask

How do I change the Ansible Python version?

In this case, you need to sudo su then install the ansible with pip3, otherwise, it will end up installing for you account only under: ~/. local/bin. By new pip version, it's recommended to use python3 -m pip install xxx than directly execute pip3 install xxx.

Which Python version is required for Ansible?

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.

Does Ansible need python3?

While you can write Ansible modules in any language, most Ansible modules are written in Python, including the ones central to letting Ansible work. By default, Ansible assumes it can find a /usr/bin/python on your remote system that is either Python2, version 2.6 or higher or Python3, 3.5 or higher.

Is Python mandatory for Ansible?

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


1 Answers

Q: "What's the invocation to do this correctly?"

- hosts: test_server
  tasks:
    - name: install pip
      ansible_python_interpreter: /usr/bin/python
      apt:
        name: python-pip

ERROR! 'ansible_python_interpreter' is not a valid attribute for a Task


A: ansible_python_interpreter is not a Playbook keyword. It is a variable and must be declared as such. For example in the scope of a task

- hosts: test_server
  tasks:
    - name: install pip
      apt:
        name: python-pip
      vars:
        ansible_python_interpreter: /usr/bin/python

, or in the scope of a playbook

- hosts: test_server
  vars:
    ansible_python_interpreter: /usr/bin/python
  tasks:
    - name: install pip
      apt:
        name: python-pip

, or in any other suitable place. See Variable precedence: Where should I put a variable?


See
  • INTERPRETER_PYTHON
  • INTERPRETER_PYTHON_DISTRO_MAP
  • INTERPRETER_PYTHON_FALLBACK
like image 57
Vladimir Botka Avatar answered Nov 15 '22 10:11

Vladimir Botka