Ansible lab setup guide

@amitmund July 11, 2026

Goal: Build a production-like Ansible lab consisting of:

  • 1 Ubuntu Ansible Control Node (Master)
  • 3 Ubuntu Managed Nodes (Clients)

This guide covers installation, SSH setup, inventory, testing, ad-hoc commands, and your first playbook.


Lab Topology

                   +----------------------+
                   |  Control Node        |
                   |----------------------|
                   | Ubuntu 24.04         |
                   | Ansible Installed    |
                   | SSH Private Key      |
                   +----------+-----------+
                              |
          --------------------------------------------
          |                  |                       |
          |                  |                       |
          ▼                  ▼                       ▼
+----------------+  +----------------+   +----------------+
| Managed Node 1 |  | Managed Node 2 |   | Managed Node 3 |
| Ubuntu         |  | Ubuntu         |   | Ubuntu         |
| SSH Server     |  | SSH Server     |   | SSH Server     |
+----------------+  +----------------+   +----------------+

Lab Requirements

Hardware

Component Recommendation
RAM 2 GB minimum per VM
CPU 2 vCPU
Disk 20 GB
OS Ubuntu Server 24.04 LTS

Virtualization

Choose one:

  • VirtualBox
  • VMware Workstation
  • Proxmox
  • KVM/QEMU
  • Hyper-V
  • Multipass
  • LXD

Lab Machines

Hostname IP Address Purpose
ansible-master 192.168.1.10 Control Node
ansible-node1 192.168.1.11 Managed Node
ansible-node2 192.168.1.12 Managed Node
ansible-node3 192.168.1.13 Managed Node

Step 1 — Install Ubuntu

Install Ubuntu Server on all four machines.

During installation:

  • Create user:
devops

Enable

  • OpenSSH Server

Verify

hostnamectl

Expected

ansible-master

or

ansible-node1

etc.


Step 2 — Configure Static IP

Edit Netplan

sudo nano /etc/netplan/*.yaml

Example

network:
  version: 2
  ethernets:
    ens33:
      addresses:
        - 192.168.1.10/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 1.1.1.1

Apply

sudo netplan apply

Verify

ip addr

Step 3 — Update All Machines

sudo apt update

sudo apt upgrade -y

Step 4 — Install OpenSSH

Verify

sudo systemctl status ssh

If missing

sudo apt install openssh-server -y

Enable

sudo systemctl enable ssh

sudo systemctl start ssh

Verify

ss -tulpn | grep 22

Step 5 — Verify Connectivity

From Control Node

ping 192.168.1.11

ping 192.168.1.12

ping 192.168.1.13

Step 6 — Install Ansible

Only on

ansible-master

Install

sudo apt update

sudo apt install ansible -y

Verify

ansible --version

Step 7 — Generate SSH Keys

On

ansible-master

Generate key

ssh-keygen

Accept defaults

Creates

~/.ssh/id_ed25519

~/.ssh/id_ed25519.pub

Step 8 — Copy Public Key

Node 1

ssh-copy-id [email protected]

Node 2

ssh-copy-id [email protected]

Node 3

ssh-copy-id [email protected]

Verify

ssh [email protected]

Should not ask password.


Step 9 — Create Inventory

Create directory

mkdir ~/ansible-lab

cd ~/ansible-lab

Create inventory

nano inventory.ini

Contents

[web]

ansible-node1 ansible_host=192.168.1.11

ansible-node2 ansible_host=192.168.1.12

[database]

ansible-node3 ansible_host=192.168.1.13

[all:vars]

ansible_user=devops

ansible_python_interpreter=/usr/bin/python3

Verify inventory

ansible-inventory -i inventory.ini --list

Step 10 — Test Ansible Connectivity

ansible all -i inventory.ini -m ping

Expected

SUCCESS

Step 11 — Gather Facts

ansible all -i inventory.ini -m setup

Displays

  • CPU
  • Memory
  • Kernel
  • IP
  • Architecture
  • Disk
  • Hostname
  • Interfaces

Step 12 — Useful Ad-hoc Commands

Hostname

ansible all -m command -a hostname

Kernel

ansible all -m command -a "uname -r"

Disk

ansible all -m shell -a "df -h"

Memory

ansible all -m shell -a "free -m"

CPU

ansible all -m shell -a "lscpu"

Uptime

ansible all -m shell -a uptime

Current User

ansible all -m shell -a whoami

Step 13 — First Playbook

Create

nano ping.yml

Contents

---
- name: Test Connectivity
  hosts: all

  tasks:

    - name: Ping Hosts
      ansible.builtin.ping:

Run

ansible-playbook -i inventory.ini ping.yml

Step 14 — Install Nginx

Create

nano nginx.yml
---
- name: Install Nginx
  hosts: web
  become: yes

  tasks:

    - name: Install Package
      apt:
        name: nginx
        state: present
        update_cache: yes

Run

ansible-playbook -i inventory.ini nginx.yml

Verify

curl http://192.168.1.11

Step 15 — Create ansible.cfg

nano ansible.cfg
[defaults]

inventory=inventory.ini

host_key_checking=False

forks=10

timeout=30

Now inventory is automatic.


Step 16 — Verify Inventory

ansible all --list-hosts

Directory Structure

ansible-lab/

├── ansible.cfg

├── inventory.ini

├── ping.yml

├── nginx.yml

└── roles/

Useful Ansible Commands

Command Description
ansible --version Show version
ansible all -m ping Test connectivity
ansible all -m setup Gather facts
ansible-playbook playbook.yml Execute playbook
ansible-inventory --list Show inventory
ansible-doc apt Module documentation
ansible-galaxy init role Create role
ansible-config dump Show configuration
ansible localhost -m debug -a "msg='Hello'" Debug message

Verify SSH

ssh [email protected]

ssh [email protected]

ssh [email protected]

Troubleshooting

Permission Denied

Permission denied (publickey)

Fix

ssh-copy-id [email protected]

Host Unreachable

Check

ping

ip addr

netplan

firewall

Python Missing

Failed to find Python

Install

sudo apt install python3 -y

SSH Timeout

Verify

systemctl status ssh

Inventory Errors

Validate

ansible-inventory -i inventory.ini --list

Security Best Practices

  • Use SSH keys instead of passwords.
  • Disable root SSH login.
  • Disable password authentication after key setup.
  • Keep Ansible and Ubuntu updated.
  • Use become: yes only when necessary.
  • Store secrets in Ansible Vault, not in playbooks.
  • Restrict SSH access using firewalls or security groups.
  • Use separate inventories for development, staging, and production.

Exercises

  1. Add a fourth managed node.
  2. Create a new inventory group named monitoring.
  3. Write a playbook to install htop.
  4. Configure passwordless SSH for a new user.
  5. Create a playbook that updates all packages.
  6. Gather and display the kernel version from every node.
  7. Install Docker on all managed nodes using a playbook.
  8. Reboot all managed nodes with Ansible.
  9. Create an Ansible role for Nginx installation.
  10. Encrypt a variable using Ansible Vault.

Interview Questions

  1. What is the difference between the Ansible Control Node and Managed Node?
  2. Why does Ansible use SSH instead of agents?
  3. What is an inventory file?
  4. What are Ansible modules?
  5. What is a playbook?
  6. What is the difference between command, shell, and raw modules?
  7. What is become: yes?
  8. What are Ansible facts?
  9. What is Ansible Vault used for?
  10. How would you organize inventories for multiple environments?

Next Learning Topics

  • Inventory Groups
  • Variables
  • Facts
  • Playbooks
  • Modules
  • Roles
  • Templates (Jinja2)
  • Handlers
  • Tags
  • Conditionals
  • Loops
  • Vault
  • Collections
  • Galaxy
  • Dynamic Inventory
  • Error Handling
  • Async Tasks
  • Delegation
  • Serial & Rolling Updates
  • Strategy Plugins
  • Custom Modules
  • CI/CD Integration
  • AWX / Ansible Automation Platform
  • Production Best Practices
0 Likes
21 Views
0 Comments

Filters

No filters available for this view.

Reset All