liveBook · Manning

Ansible playbook

As explained in Road to Kubernetes

Ansible playbook is a YAML-based automation definition that describes hosts, variables, and ordered tasks for configuring systems or deploying applications.

An Ansible playbook is a YAML file that declares the desired state of remote machines. Instead of manually installing and configuring an application on each machine, you describe the result you want—such as NGINX installed and running—and Ansible applies the tasks to the selected hosts. section 4.2 section 4.2.2

A basic playbook names the work, selects `hosts` from an inventory, and lists `tasks` in execution order. `become: true` lets tasks run as root; `vars` can hold reusable values. A task can install a package, copy a configuration file, or set a service’s state. For example, `state: present` requests an installed package, while `state: started` requests a running service. section 4.2.2 section 4.2.4

Start with NGINX

Create `devops/ansible/install-nginx.yaml` with `hosts: all`, `become: true`, and an `apt` task requesting `nginx` with `state: present`. Add a `service` task requesting `state: started` and `enabled: yes`. Running the playbook reports each task’s status for each host, so you can see whether Ansible made a change. section 4.2.2

Listing 4.7 Ensuring NGINX is running with Ansible
- name: Install NGINX
  hosts: all
  become: true
  tasks:
  - name: Install NGINX
    apt:
      name: nginx
      state: present
      update_cache: yes
  - name: Ensure NGINX is running
    service:
      name: nginx
      state: started
      enabled: yes

The complete small playbook shows how an ordered task list both installs NGINX and ensures its service is running. section 4.2.2

To run a playbook from GitHub Actions, set up Python and Ansible, provide an SSH private key, create an inventory of remote hosts, and configure Ansible to use that inventory. A connection check with `ansible all -m ping` can verify the setup before `ansible-playbook` runs. The inventory, not the playbook’s task list, determines which machines belong to a selected host group. section 4.2.1 section 4.2.2

The same pattern grows from service setup into application deployment:

  • For a Python application, update packages, install dependencies, create a virtual environment and directories, copy source files, install `requirements.txt`, then copy Supervisor and NGINX configurations. Configuration tasks can notify handlers to reload Supervisor or restart NGINX. section 4.2.3
  • For a Node.js application, reuse the deployment pattern with a `vars` value for Node.js 18, copy the application and `package.json`, run `npm install`, and configure Supervisor and NGINX. section 4.2.4

This makes a playbook useful beyond a one-time setup command: the same declared tasks can be run for one host or many. These examples stay with basic playbooks rather than Ansible’s full range of options. section 4.2.2 section 4.2.1

How Road to Kubernetes covers it

How deep it goes
Builds basic playbooks through ordered deployment tasks and handlers, while deferring advanced Ansible options and detailed permission management.
Examples
Works from an NGINX install-and-start playbook to Python and Node.js deployment playbooks.
What it assumes
Teaches playbook fields and inventory terminology from the start, while requiring SSH keys for access to remote hosts.
What you can do after
Build it from scratch: write and run basic playbooks that install dependencies, deploy application files, and configure services.
How it connects
Connects desired-state configuration with inventories, SSH, GitHub Actions, NGINX, Supervisor, and application deployment.

Also explained in

Compare how 2 books explain Ansible playbook →

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.