Vagrant is one of the favorite tools for Developers. Vagrant is a tool designed to enable users create and configure lightweight, reproducible, and portable development environments with the Virtualization environments of their choice – VirtualBox, KVM, VMware e.t.c. So how can I use Ansible with Vagrant for clean automation?.

You can use Ansible with Vagrant to automate provisioning of Vagrant machines requirements and setup software dependencies. This guide won’t go into so much detail about Vagrant and Ansible, but just an overview on how you can use Ansible with Vagrant.

I’ll a create a project directory.

mkdir ~/myvagrant
cd  ~/myvagrant

I’ll create a Simple Ansible Playbook to update the OS and install basic packages. You can extend to cover what you need to setup.

$ vim provision.yaml
---
- hosts: all
  become: yes
  become_method: sudo
  tasks:
 - name: Update OS
   package:
    name: '*'
    state: latest

  - name: Install Basic packages
    package:
      name: ['vim', 'zip', 'bash-completion', 'wget', 'tmux']

Then create a Vagrantfile with a definition of ansible provisioner.

$ vim Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'libvirt'
Vagrant.configure("2") do |config|
  ##### VM definition #####
  config.vm.define "rocky" do |config|
  config.vm.hostname = "rocky"
  config.vm.box = "generic/rocky8"
  config.vm.box_check_update = false
  config.vm.provision :ansible do |ansible|
    ansible.limit = "all"
    ansible.playbook = "provision.yaml"
  end
  config.vm.provider :libvirt do |v|
    v.memory = 1024
    v.cpus = 1
    end
  end
end

As can be seen in our Vagrantfile, we’re using provision.yaml Playbook.

Once you have both Playbook and Vagrantfile ready, run the vagrant up command to start the Virtual Machine.

$ vagrant up
Bringing machine 'rocky' up with 'libvirt' provider...
==> rocky: Creating image (snapshot of base box volume).
==> rocky: Creating domain with the following settings...
==> rocky:  -- Name:              tmp_rocky
==> rocky:  -- Description:       Source: /tmp/Vagrantfile
==> rocky:  -- Domain type:       kvm
==> rocky:  -- Cpus:              1
==> rocky:  -- Feature:           acpi
==> rocky:  -- Feature:           apic
==> rocky:  -- Feature:           pae
==> rocky:  -- Clock offset:      utc
==> rocky:  -- Memory:            1024M
==> rocky:  -- Base box:          generic/rocky8
==> rocky:  -- Storage pool:      images
==> rocky:  -- Image(vda):        /var/lib/libvirt/images/tmp_rocky.img, virtio, 128G
==> rocky:  -- Disk driver opts:  cache='default'
==> rocky:  -- Graphics Type:     vnc
==> rocky:  -- Video Type:        cirrus
==> rocky:  -- Video VRAM:        256
==> rocky:  -- Video 3D accel:    false
==> rocky:  -- Keymap:            en-us
==> rocky:  -- TPM Backend:       passthrough
==> rocky:  -- INPUT:             type=mouse, bus=ps2
==> rocky: Creating shared folders metadata...
==> rocky: Starting domain.
==> rocky: Domain launching with graphics connection settings...
==> rocky:  -- Graphics Port:      5900
==> rocky:  -- Graphics IP:        127.0.0.1
==> rocky:  -- Graphics Password:  Not defined
==> rocky:  -- Graphics Websocket: 5700
==> rocky: Waiting for domain to get an IP address...
==> rocky: Waiting for machine to boot. This may take a few minutes...
    rocky: SSH address: 192.168.121.12:22
    rocky: SSH username: vagrant
    rocky: SSH auth method: private key
    rocky:
    rocky: Vagrant insecure key detected. Vagrant will automatically replace
    rocky: this with a newly generated keypair for better security.
    rocky:
    rocky: Inserting generated public key within guest...
    rocky: Removing insecure key from the guest if it's present...
    rocky: Key inserted! Disconnecting and reconnecting using new SSH key...
==> rocky: Machine booted and ready!
==> rocky: Setting hostname...
==> rocky: Running provisioner: ansible...

If you had your Vagrant machine up, then use the command:

$ vagrant provision
==> rocky: Running provisioner: ansible...
PLAY [all] *********************************************************************

TASK [Gathering Facts] *********************************************************
ok: [rocky]

TASK [Update OS] ***************************************************************

changed: [rocky]

TASK [Install Basic packages] **************************************************
changed: [rocky]

PLAY RECAP *********************************************************************
rocky                      : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

Our execution was successful, you can now ssh to the VM with the command:

$ vagrant ssh
Last login: Fri Sep 15 08:41:59 2023 from 192.168.121.1
[vagrant@rocky ~]$

Read through the Vagrant Provisioning guide to learn more about supported provisioners and how to use them.

More guides:

LEAVE A REPLY

Please enter your comment!
Please enter your name here