How to Work with Ansible Provisioner in Vagrant

In the previous article, we have seen what is provisioner and how to work with shell and file provisioner. In this article, we will see how to use an ansible provisioner to automate the vagrant workflow. Here we will create a playbook and use the playbook to run ansible play against the vagrant guest machine.

The following is the snippet from the playbook I am going to use in this article for demonstration. There is only one play and it will take care of installing packages using the apt module. Create a new .yml or .yaml file and copy the snippet.

---
- hosts: all
  become: yes
  tasks:
    - name: Install 4 packages
      apt:
        pkg:
        - net-tools
        - neofetch
        - cowsay
        - htop
        update_cache: yes

How to Work with Ansible Remote Provisioner in Vagrant

To run playbooks, ansible should be installed in the vagrant guest virtual machine. You can use a shell provisioner to install ansible. Refer to the below code, there is a section for shell provisioner and I am using series of apt package manager commands to set up an ansible repository and install it.

agrant.configure("2") do |config|
  config.vm.box = "ubuntu/focal64"
	config.vm.hostname = "ansible-provisioner"
	config.vm.network "public_network", bridge: "Intel(R) Dual Band Wireless-AC 7265", ip: "192.168.1.100"
	config.vm.provider "virtualbox" do |vbox|
        	vbox.memory = "2048"
  	vbox.cpus = "2"
	end
  config.vm.provision "shell", inline: <<-BLOCK
	sudo apt-add-repository ppa:ansible/ansible -y
	sudo apt update
	sudo apt install ansible -y
	ansible --version
  BLOCK
end

Create a new project directory and run the vagrant init command.

$ mkdir ansible_provisioner 
$ cd ansible_provisioner
$ vagrant init -m

Now depending upon the state of your virtual machine (running or halt) you can run the following command. If this is the first time then vagrant up will do the job.

$ vagrant up  ==> Running VM for first time
$ vagrant reload --provision  ==> If VM is already provisioned and running
$ vagrant up --provision ==> VM is already provisioned and not running

During the virtual machine boot process, you can see from the logs ansible is getting installed.

Ansible Installation in Virtual Machine
Ansible Installation in Virtual Machine

Now you are good to use the playbook. I will modify the same configuration file and add an ansible, remote provisioner. Below is the syntax. Playbook path (absolute or relative) to be provided as shown below.

config.vm.provision "ansible" do |ans|
	ans.playbook = "my_playbook.yml"    # Playbook in same folder as Vagranfile
	#ans.playbook = "/home/karthick/vagrant/ansible_provisioner/my_playbook.yml"

From the below output you can see my play executed successfully.

Running Ansible Playbook in Virtual Machine
Running Ansible Playbook in Virtual Machine

How to Use Ansible Local Provisioner in Vagrant

This is another method in setting up ansible in your vagrant guest VM. When you use a local provisioner vagrant will take care of installing the ansible for you automatically. The only input you need to provide is through which method vagrant should set up ansible.

Below is the syntax for the local provisioned.

config.vm.provision "ansible_local" do |ans|
  	ans.playbook = "my_playbook.yml"

There are three different modes.

  1. default – Installing ansible using the distribution-based package manager.
  2. PIP – Installing ansible using PIP.
  3. PIP Args – Installing using PIP args.

By default, the vagrant will try to install from the repository. If you wish to install via PIP you can set the syntax as below. When you use PIP, the vagrant will first check if PIP is available in the guest machine and it will try to install it. Once PIP is available it will use PIP to download and install an ansible package from PyPI.

config.vm.provision "ansible_local" do |ans|
  	ans.playbook = "my_playbook.yml"
  	ans.install_mode = "pip"

That’s it for this article. In the next article, we will see how to create custom boxes in vagrant.

1 thought on “How to Work with Ansible Provisioner in Vagrant”

  1. In the first section, when you use:

    config.vm.provision "ansible"
    

    You will not be using the version of ansible or binary that you installed into the guest with the shell provisioner.

    For that part, to work you must have had ansible installed on the Vagrant host. Only ansible_local will use ansible directly on the guest.

    Reply

Got something to say? Join the discussion.