Michael Trojanek (relativkreativ) — Bootstrapper and creator of things

This article was published on January 26th 2019 and takes about 4 minutes to read.

How to install Python with Ansible

Ansible depends on Python to do its magic. Nonetheless it is possible to install Python on a server and thus prepare it for Ansible provisioning — with Ansible.

Watching Ansible provision a server from top to bottom and then deploy our Rails application is not onlyincredibly rewarding but also a safety net in case of desaster: It means that we can get back to a working state in a matter of minutes.

Before Ansible can take over, a Python interpreter must be present on the target machine. Manually installing Python is error prone and — if we need to do it regularly or on multiple machines — boring as hell.

Fortunately, it is possible to use Ansible to install Python on our server even when it actually depends on it — Ansible provides the raw module which can run basic low-down SSH commands on our server even if Python is not available.

Prepare a dedicated play

It makes sense to create a dedicated play for bootstrapping (we may even call it bootstrap.yml), for two reasons:

- hosts: all
  remote_user: root
  gather_facts: false
  tasks:

Install Python 2

Even though Ansible claims to be compatible with Python 3, some modules still have glitches — it is safer to use Ansible 2.

Just because we do not have Python at our disposal yet does not mean that we cannot keep our tasks idempotent — we should always do that. So the first thing we do is checking whether Python is already installed or not:

    - name: Check for Python
      raw: test -e /usr/bin/python
      changed_when: false
      failed_when: false
      register: check_python

This task runs the command test -e /usr/bin/python on our server which checks for the /usr/bin/python binary's existence. This works because only the Python 2 binary is called python (Python 3's interpreter is called python3).

We save this tasks's result using Ansible's register directive. We use this result to judge whether Python's installation is necessary in the next task.

How we install Python depends on whether our server runs CentOS or Ubuntu. On CentOS systems we use yum:

    - name: Install Python
      raw: yum -y install python
      when: check_python.rc != 0

On Ubuntu systems we use the apt command (and we have to update its cache):

    - name: Install Python
      raw: apt -y update && apt install -y python-minimal
      when: check_python.rc != 0

If we need to support both CentOS and Ubuntu systems in a single play, we can apply this tasks for both operating systems in one fell swoop:

    - name: Install Python
      raw: test -e /usr/bin/apt && (apt -y update && apt install -y python-minimal) || (yum -y install python libselinux-python)
      when: check_python.rc != 0

This works by first testing for the presence of the /usr/bin/apt command which is only present on Ubuntu systems.

By chaining subsequent commands with &&, we make sure that they only run when this check evaluates to true. The yum command after the ||, on the other hand, is only run when the test evaluates to false, meaning the apt command is not available (which only happens on CentOS systems).

Call the bootstrap play from our main playbook

Using Ansible's import_playbook directive, we can now import this play into any playbook:

---

- import_playbook: bootstrap.yml

Once our bootstrap play has completed, we can be sure that Python is available and we may continue to provision our server using any of Ansible's wealth of modules.

The article you just read is a simplified version of one of the many useful techniques illustrated in my book Efficient Rails DevOps. Head over to its website to grab yourself a copy of the free sample chapter including its table if contents.

Get in the loop!

Like this article? There's more where this one's coming from — join my email list to get new articles delivered straight into your inbox.

I'll send you a free copy of my book Build Your Own Rails Server as a welcome gift.

No spam — guaranteed. You can leave at any time.