Skip to main content
RunBook Academy

LinuxXXXIV · Configuration ManagementAnsible

Ansible for sysadmins - the modern CM tool

Intermediate⏱ ~12 minansible

What you'll learn

  • Install and configure Ansible
  • Write a basic playbook
  • Use modules for common tasks
  • Run the playbook against a fleet

Prerequisites

Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09

Not yet marked complete on this device.

Ansible is the most common configuration management tool for sysadmins. It is agentless (uses SSH), declarative (YAML), and has a large module ecosystem.

Install Ansible

# Debian/Ubuntu
sudo apt install ansible

# RHEL family
sudo dnf install ansible-core

# Or via pip
pip install ansible

Configure inventory

/etc/ansible/hosts:

[webservers]
web01.example.com
web02.example.com
web03.example.com

[databases]
db01.example.com

[production:children]
webservers
databases

Or in YAML:

all:
  children:
    webservers:
      hosts:
        web01.example.com:
        web02.example.com:
        web03.example.com:
    databases:
      hosts:
        db01.example.com:

First playbook

# ping.yml
- name: Ping all hosts
  hosts: all
  tasks:
    - name: Ping
      ansible.builtin.ping:

Run:

ansible-playbook -i inventory ping.yml

Modules

Modules are the building blocks. Common ones:

# Package
- apt:
    name: nginx
    state: present
  become: yes

# Service
- service:
    name: nginx
    state: started
    enabled: yes
  become: yes

# File
- copy:
    src: files/nginx.conf
    dest: /etc/nginx/nginx.conf
    owner: root
    group: root
    mode: '0644'
  become: yes

# User
- user:
    name: deploy
    state: present
    shell: /bin/bash
  become: yes

# Command (only when no module exists)
- command: /usr/bin/foo --bar
  become: yes

Variables and templates

- hosts: webservers
  vars:
    nginx_worker_processes: 4
    nginx_listen_port: 80
  tasks:
    - template:
        src: nginx.conf.j2
        dest: /etc/nginx/nginx.conf
      become: yes

nginx.conf.j2:

worker_processes {{ nginx_worker_processes }};
listen {{ nginx_listen_port }};

Handlers

Run only on change:

- name: Configure nginx
  template:
    src: nginx.conf.j2
    dest: /etc/nginx/nginx.conf
  become: yes
  notify: reload nginx

handlers:
  - name: reload nginx
    service:
      name: nginx
      state: reloaded
    become: yes

Handlers run at the end of the playbook, only if a task notified them.

Roles

Organise playbooks:

roles/
  nginx/
    tasks/main.yml
    handlers/main.yml
    templates/nginx.conf.j2
    files/index.html
  postgresql/
    tasks/main.yml

Use:

- hosts: webservers
  roles:
    - nginx

Knowledge check

Knowledge check · 3 questions

  1. Q1. Which file is the Ansible inventory by default?

  2. Q2. Ansible requires an agent on managed hosts.

  3. Q3. Which of the following are Ansible modules? Select all that apply.

Passing score: 75%. Answers are checked in this browser.