Michael Trojanek (relativkreativ) — Bootstrapper and creator of things

This is one of my most popular articles. It was published on January 26th 2019 and takes about 4 minutes to read.

Use it with caution — it is probably still valid, but it has not been updated for over a year.

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:

  • We need to run its tasks as root (even if we plan to provision our server with a different user) because our server has to be considered fresh out of the box and therefore probably has no other users yet.
  • We need to turn off Ansible's fact gathering with gather_facts: false. Fact gathering relies on Python so our play will crash if it is not already installed.
- hosts: all
  remote_user: root
  gather_facts: false

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

Join my email list to get new articles delivered straight to your inbox and discounts on my products.

No spam — guaranteed.

You can unsubscribe at any time.

Got it, thanks a lot!

Please check your emails for the confirmation request I just sent you. Once you clicked the link therein, you will no longer see these signup forms.