Ansible Lab Setup with KVM - Lab
Learn how to configure Ansible inventories using YAML files. This guide covers essential keys, default groups, and practical examples for network automation.
Ansible Lab Setup with KVM
Ansible is a tool used for automating tasks like configuring servers, managing networks, or deploying applications. It’s agentless, meaning it doesn’t require installation on remote devices if Python and SSH are available. For network devices, Ansible may connect via APIs or other protocols.
How Ansible Works
Ansible works in steps:
- Creates a script for the task.
- Copies the script to the remote device.
- Runs the script on the remote device.
- Removes the script after completion.
Key points:
- Tasks run in order across all devices.
- Ansible waits for all devices to complete one task before moving to the next.
Why Use Ansible?
- Easy to understand syntax.
- No need to install anything on remote devices.
- Uses push-based architecture—commands are sent from the control node.
- Comes with built-in modules for different tasks.
Control Node
- A system with Python 3.5 or higher (e.g., Fedora, Ubuntu, RHEL, etc.).
Managed Nodes
- Devices accessible via SSH. Python should be installed unless the device is a network switch/router.
Lab Setup
This lab focuses on setting up a control node and managed nodes using KVM.
- Install QEMU/KVM on your host machine (e.g., Fedora 39/Ubuntu 22).
- Create virtual machines:
- Fedora 39
- Ubuntu 22
Setting Up the Lab
- Edit the
/etc/hosts
file to map IPs to hostnames on your control node:
1
sudo nano /etc/hosts
Add:
1
2
192.168.122.10 f39s
192.168.122.11 u22s
- Set up SSH keys for passwordless login from your control node to managed nodes:
1
2
3
ssh-keygen
ssh-copy-id -i .ssh/id_rsa.pub zulu@f39s
ssh-copy-id -i .ssh/id_rsa.pub zulu@u22s
- Allow passwordless sudo on your managed nodes:
- Log into each managed node.
- Edit the sudoers file:
1
sudo visudo
- Add:
1
zolo ALL=(ALL) NOPASSWD: ALL
Installing Ansible
For detailed instructions on installing Ansible, refer to the Ansible Installation guide, which covers both apt
and pip
methods.
Configuring Managed Nodes
Create an inventory file to list managed nodes. For more details on inventory, refer to the Ansible Inventory Documentation.
Example hosts
file:
1
nano hosts
Add:
1
2
3
4
5
[fedora]
f39s ansible_user=zolo
[ubuntu]
u22s ansible_user=zolo