Ansible is a powerful automation tool that simplifies managing Linux servers at scale using declarative, desired state configurations. It ensures seamless, consistent, and reproducible server configurations, making infrastructure management efficient and error-free.
As an example, manually setting up and managing multiple Apache servers can be a daunting and time-consuming task. Configuring each server, installing dependencies, and ensuring services are running properly can quickly become tedious. This is where Ansible comes in – a powerful automation tool that streamlines server configuration, deployment, and management, making the process efficient, repeatable, and hassle-free.
Mastering KVM Virtualization - The Ultimate eBook
From home labs to production clouds - master KVM Host management, automating KVM administration using Terraform, Vagrant, and cloud automation. This eBook will enable you to build scalable virtual infrastructure that works whether you're learning at home or deploying enterprise solutions. Get your full copy today
Installing Ansible
Install ansible on your Ubuntu system from PPA or official operating system repositories. This example uses PPA for latest release installation.
sudo apt update
sudo apt-add-repository ppa:ansible/ansible
sudo apt install ansible
Successful installation of ansible can be checked by getting version.
ansible --version
You should get an output like below:
ansible [core 2.17.9]
config file = /etc/ansible/ansible.cfg
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3/dist-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/bin/ansible
python version = 3.12.3 (main, Feb 4 2025, 14:48:35) [GCC 13.3.0] (/usr/bin/python3)
jinja version = 3.1.2
libyaml = True
Create a playbook
This playbook installs the Apache web server on an Ubuntu machine provisioned by Vagrant. For a more in-depth understanding of Ansible concepts like roles, collections, and best practices, refer to the official Ansible documentation. These topics are beyond the scope of this example.
In the directory where Vagrantfile
lives, create a playbook file named apache-playbook.yaml
nano playbook.yml
Paste the following contents into the file.
---
- name: Configure Apache Web Server on Ubuntu (Vagrant)
hosts: all
become: yes
tasks:
- name: Ensure Apache is installed
apt:
name: apache2
state: present
update_cache: yes
- name: Start and enable Apache service
service:
name: apache2
state: started
enabled: yes
- name: Ensure Apache is listening on port 80
lineinfile:
path: /etc/apache2/ports.conf
regexp: '^Listen '
line: 'Listen 80'
notify: Restart Apache
- name: Allow HTTP traffic through UFW firewall
ufw:
rule: allow
port: '80'
proto: tcp
- name: Ensure UFW is enabled
ufw:
state: enabled
policy: allow
handlers:
- name: Restart Apache
service:
name: apache2
state: restarted
Create or modify existing Vagrantfile
to add ansible playbook provisioning.
Vagrant.configure("2") do |config|
# Set the hostname for the VM
config.vm.hostname = "webserver.cloudspinx.com"
# Define the VM named "webserver"
config.vm.define "webserver" do |webserver|end
# Specify the base box
config.vm.box = "alvistack/ubuntu-24.04"
config.vm.box_check_update = true
# Configure the provider settings
config.vm.provider :libvirt do |libvirt, override|
libvirt.cpu_mode = "host-passthrough"
libvirt.cpus = 2
libvirt.memory = 4096
libvirt.disk_bus = "virtio"
libvirt.disk_driver :cache => "writeback"
libvirt.driver = "kvm"
libvirt.nested = true
libvirt.nic_model_type = "virtio"
end
# Provisioning using ansible
config.vm.provision "ansible" do |ansible|
ansible.playbook = "playbook.yml"
end
end
Start vagrant machine. If up stop before start.
vagrant up
During instance creation and provisioning you will see playbook get executed:
TASK [Gathering Facts] *********************************************************
==> web: Running provisioner: ansible...
ok: [webserver]
TASK [Ensure Apache is installed] **********************************************
web: Running ansible-playbook...
PLAY [Configure Apache Web Server on Ubuntu (Vagrant)] *************************
TASK [Gathering Facts] *********************************************************
ok: [web]
TASK [Ensure Apache is installed] **********************************************
changed: [webserver]
TASK [Start and enable Apache service] *****************************************
ok: [webserver]
TASK [Ensure Apache is listening on port 80] ***********************************
ok: [webserver]
TASK [Allow HTTP traffic through UFW firewall] *********************************
changed: [webserver]
TASK [Ensure UFW is enabled] ***************************************************
changed: [web]
TASK [Start and enable Apache service] *****************************************
ok: [web]
TASK [Ensure Apache is listening on port 80] ***********************************
ok: [web]
TASK [Allow HTTP traffic through UFW firewall] *********************************
changed: [webserver]
PLAY RECAP *********************************************************************
webserver : ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
changed: [web]
TASK [Ensure UFW is enabled] ***************************************************
changed: [web]
PLAY RECAP *********************************************************************
web : ok=6 changed=3 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Login to the instance once it’s created.
vagrant ssh
Check the status of apache2
service
systemctl status apache2
It should display as running:
● apache2.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/apache2.service; enabled; preset: enabled)
Active: active (running) since Fri 2025-03-14 06:27:42 UTC; 6min ago
Docs: https://httpd.apache.org/docs/2.4/
Main PID: 2818 (apache2)
Tasks: 55 (limit: 4608)
Memory: 5.2M (peak: 5.4M)
CPU: 141ms
CGroup: /system.slice/apache2.service
├─2818 /usr/sbin/apache2 -k start
├─2820 /usr/sbin/apache2 -k start
└─2821 /usr/sbin/apache2 -k start
Vagrant management commands with examples
This section highlights commonly used vagrant commands to help you work more efficiently with Vagrant and Libvirt.
Print vagrant version
Print the current version of installed vagrant
vagrant version
Manage vagrant boxes
Download vagrant box
vagrant box add --provider libvirt <box-name-or-url>
List downloaded boxes
vagrant box list
Delete vagrant box
vagrant box remove <box>
Prune older boxes
vagrant box prune
Update a specific box
# Inf Vagrantfile
vagrant box update
# Named box
vagrant box update --box <box>
Repackage box
vagrant box repackage <name> <provider> <version>
Validate Vagrantfile
Validate the configurations in your Vagrantfile by running:
vagrant validate
Start / provision vagrant machine
Start and provision the vagrant environment
vagrant up
To provision the vagrant machine use
vagrant provision
Display information about machines
Display information about all known Vagrant environments on the machine
vagrant global-status
Connect to machine via SSH
To connect to a single instance machine defined in a Vagrantfile
vagrant ssh
If there are multiple machines, specify the name
vagrant ssh <name|id>
It’s also possible to output OpenSSH valid configuration to connect to the machine
vagrant ssh-config
Suspend vagrant machine
A vagrant machine can be suspended by executing the command:
vagrant suspend
Restart vagrant machine
Restart a vagrant machine and load new Vagrantfile configuration
vagrant reload
Resume suspended machine
To resume a suspended vagrant machine, run:
vagrant resume
Stopping Vagrant machine(s)
vagrant halt
VM snapshots management
Create a VM snapshot
vagrant snapshot save <vm-name> <snapshot-name>
List snapshots
vagrant snapshot list <vm-name>
Restore snapshot
vagrant snapshot restore <vm-name> <snapshot>
Delete snapshot
vagrant snapshot delete <vm-name> <snapshot>
- Destroy vagrant machine(s)
vagrant destroy
Agree to destroy the VM using y
key:
ubuntu24: Are you sure you want to destroy the 'ubuntu24' VM? [y/N] y
==> ubuntu24: Removing domain...
==> ubuntu24: Deleting the machine folder
- Remove vagrant boxe(s)
vagrant box remove <box-name>
- Stop and delete all traces of the vagrant machine
# Gracefully
vagrant destroy --graceful <name>
# Forcefully
vagrant destroy --force <name>