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.
What You Need
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
- Install QEMU/KVM on your host machine (e.g., RHEL 9).
- Create virtual machines:
- Fedora 39
- Ubuntu 22
Setting Up the Lab
- Edit the
/etc/hosts
file to map IPs to hostnames:
sudo nano /etc/hosts
Add:
192.168.122.10 f39s
192.168.122.11 u22s
- Set up SSH keys for passwordless login:
ssh-keygen
ssh-copy-id -i .ssh/id_rsa.pub zolo@f39s
ssh-copy-id -i .ssh/id_rsa.pub zolo@u22s
- Allow passwordless sudo:
- Log into each managed node.
- Edit the sudoers file:
sudo visudo
- Add:
zolo ALL=(ALL) NOPASSWD: ALL
Installing Ansible
Install Ansible on the control node using pip
:
pip3 install ansible-core
Verify the installation:
ansible --version
Configuring Managed Nodes
Create an inventory file to list managed nodes:
nano hosts
Add:
[fedora]
f39s ansible_user=zolo
[ubuntu]
u22s ansible_user=zolo
Running Ad-Hoc Commands
Ad-hoc commands are simple, one-time tasks you can run without writing a playbook.
Examples
- Ping all hosts:
ansible -m ping all
- Check uptime:
ansible all -a "uptime"
- Run commands as root:
ansible all -b -a "whoami"
Key Points
- Use
-m
to specify a module (e.g.,ping
,command
). - Add
-b
to run commands as root.
Understanding Ansible Modules
Modules are like tools for specific tasks.
Module | Purpose | Example |
---|---|---|
ping |
Test if devices are reachable. | ansible -m ping all |
command |
Run simple commands (no pipes or redirects). | ansible all -m command -a "uptime" |
shell |
Run commands with pipes or redirection. | ansible all -m shell -a "echo hello" |
raw |
Run commands on devices without Python. | ansible all -m raw -a "ls" |
Viewing Module Documentation
To learn about a module, use:
ansible-doc <module_name>
Example:
ansible-doc ping
Press q
to exit.
Next Steps
Once you’re comfortable running ad-hoc commands, you can move on to writing playbooks to automate complex tasks. Ansible’s simplicity and versatility make it a great choice for beginners and advanced users alike!