Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to install a Python version on server using Ansible

I am using ansible to connect with server. But I am getting errors for certain pip packages because of older version of python. How can I install a specific version of python (2.7.10) using ansible. Current python version on the server is 2.7.6

For now I have compiled and installed the python version manually but would prefer to have a way to do it via ansible.

like image 640
Ankit Avatar asked Mar 14 '23 00:03

Ankit


1 Answers

Additionally to @Simon Fraser's answer, the following playbook is what I use in Ansible to prepare a server with some specific Python 3 version:

# python_version is a given variable, eg. `3.5`
- name: Check if python is already latest
  command: python3 --version
  register: python_version_result
  failed_when: "{{ python_version_result.stdout | replace('Python ', '') | version_compare(python_version, '>=') }}"

- name: Install prerequisites
  apt: name=python-software-properties state=present
  become: true

- name: Add deadsnakes repo
  apt_repository: repo="ppa:deadsnakes/ppa"
  become: true

- name: Install python
  apt: name="python{{ python_version }}-dev" state=present
  become: true

I have the above in a role too, called ansible-python-latest (github link) in case you are interested.

like image 63
Wtower Avatar answered Mar 24 '23 19:03

Wtower