Skip to main content
RunBook Academy

← All labs in Linux

Lab · intermediate · ~45 min

Lab: Ansible basics - apply a configuration to multiple hosts

B · Nested virtualisationC · Simulation

Objectives

  • Install Ansible
  • Write an inventory
  • Apply a playbook
  • Verify idempotency

Prerequisites

This lab sets up Ansible and applies a basic playbook to multiple hosts. By the end you will have the discipline for production Ansible use.

Tasks

Task 1: Install Ansible

sudo apt install ansible
ansible --version

Task 2: Build inventory

mkdir -p ~/ansible-lab
cd ~/ansible-lab

cat > inventory.ini <<EOF
[webservers]
localhost ansible_connection=local

[all:vars]
ansible_python_interpreter=/usr/bin/python3
EOF

Task 3: Test connectivity

ansible -i inventory.ini all -m ping

Task 4: Write a playbook

# site.yml
- name: Configure all hosts
  hosts: all
  become: yes
  tasks:
    - name: Ensure vim is installed
      apt:
        name: vim
        state: present
        update_cache: yes

    - name: Ensure /etc/motd has the right content
      copy:
        content: |
          Welcome to {{ ansible_hostname }}
          Managed by Ansible.
        dest: /etc/motd
        owner: root
        group: root
        mode: '0644'

Task 5: Apply

ansible-playbook -i inventory.ini site.yml

Task 6: Verify idempotency

ansible-playbook -i inventory.ini site.yml

Output should show changed=0.

Task 7: Check mode

ansible-playbook -i inventory.ini site.yml --check

Task 8: Document

ANSIBLE LAB
===========
Inventory: ~/ansible-lab/inventory.ini
Playbook: ~/ansible-lab/site.yml

Hosts managed: localhost (1)
Tasks applied:
- vim installed
- /etc/motd created

Idempotency: verified (re-run = changed=0)

Deliverables

  • · An inventory file
  • · A playbook
  • · Verification that re-running is a no-op

Verification status

Last reviewed
2026-08-09
Executed end to end
not yet run on hardware

The commands and configuration here have been reviewed against the verified software versions, but nobody has run this lab start to finish on a system meeting its prerequisites. Treat the Expected Outcome as the intended result rather than an observed one, and keep the Cleanup section to hand.