liveBook · Manning

Ansible

As explained in Road to Kubernetes

Ansible is an open-source automation tool for configuring systems and deploying applications.

Installing NGINX with a remote `apt` command works once. Doing it again on another server raises more questions: Is NGINX already installed? Is it configured and running? Does a configuration change require a restart? Ansible replaces a growing sequence of manual commands with a repeatable description of how the machine should be set up. section 4.2 section 4.1.5

Ansible is an infrastructure-as-code tool for configuring remote machines. Its YAML files, called playbooks, declare a *desired state*—for example, that a package is present or a service has the right configuration. Ansible connects over SSH, so the machine running it needs access to the target machine through the appropriate SSH keys. section 4.2 section 4.0

A Python application deployment shows how a playbook turns that goal into tasks:

  • Update the package cache, then use Ansible’s `apt` task to require Python dependencies, NGINX, and Supervisor with `state: present`. section 4.2.3
  • Create a Python virtual environment and application directories; copy the application to the server and install packages from `requirements.txt`. section 4.2.3
  • Copy Supervisor and NGINX configuration files. Configuration tasks notify handlers that reload Supervisor or restart NGINX when needed. section 4.2.3
Listing 4.12 Installing Python with Ansible Playbook, part 3
    - name: Install dependencies
      apt:
        pkg:
          - python3-pip
          - python3-dev
          - python3-venv
          - rsync
          - nginx
          - supervisor
        state: present

The dependency task names the required packages and declares them `present`, rather than expressing installation as a one-off shell command. section 4.2.3

Listing 4.17 Installing Python with Ansible Playbook, part 8
    - name: Configure gunicorn and uvicorn with supervisor
      copy:
        src: '{{ playbook_dir }}/../../conf/supervisor.conf'
        dest: /etc/supervisor/conf.d/roadtok8s-py.conf
      notify: reload supervisor
    - name: Configure nginx
      copy:
        src: '{{ playbook_dir }}/../../conf/nginx.conf'
        dest: /etc/nginx/sites-available/roadtok8s-py
      notify: restart nginx

These copy tasks place the application’s service configurations on the remote machine and notify the relevant service handlers. section 4.2.3

Run the deployment from GitHub Actions

After defining the playbook, a GitHub Actions workflow can run `ansible-playbook deploy-python.yaml` from its Ansible directory. The workflow starts the deployment; Ansible handles the remote machine’s configuration. This is more robust than extending the workflow with many separate SSH and file-copy commands. section 4.2.3 section 4.1.5

How Road to Kubernetes covers it

How deep it goes
Builds a Python deployment playbook task by task, including packages, files, and handlers, while explicitly leaving Ansible’s wider options unexplored.
Examples
Uses NGINX installation and a Python application playbook with an apt package list, configuration files, handlers, and a GitHub Actions command.
What it assumes
Requires SSH access and appropriate keys for the remote machine; introduces playbooks and desired state before building deployment tasks.
What you can do after
Build it from scratch: assemble a Python deployment playbook and run it through a GitHub Actions workflow.
How it connects
Connects infrastructure as code and desired state to SSH, GitHub Actions, NGINX, and Supervisor.

Also explained in

Compare how 2 books explain Ansible →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Road to Kubernetes. The section links point to the parts of the book it draws on.