Managing Vagrant Instances With Ansible | Ansible 101

DevOps with Erdenay
5 min readMar 11, 2021

--

What is Vagrant?
In some of GNU/Linux distributions, Windows and Mac based OS we can have easily and reusable environments(instances) with Vagrant.
Vagrant is a machine virtuoso with virtualization tech.
For using Vagrant you need to have virtualization provider such ah VirtualBox, VMWare… And Vagrant is using Vagrantfile for creating the architecture of VMs, like you are typing “i want to have 4 Ubuntu VMs and their IP addresses should be like…” when you end your task with Vagrantfile, after “vagrant up” command in CLI that gives you all you need. I prepared some exercises and there is a Vagrantfile with 5 instances within.
After we had these instances we will manage them with Ansible, we will tell to Ansible what we want, and that will prepare the environments for us.
By the way if you are configuring your environments with a tool, its called “Configuration Management” and there is some famous different tools in market like Puppet, Chef, Saltstack and Ansible.

What is Ansible?
If we compare Ansible with other Configuration Management tools, Ansible is agentless tool. So what its mean? If we say “we have got 5 instances and i want to manage them with Ansible”, you dont need to setup Ansible to every single instance, you just need to have SSH connection with them and thats all. In Ansible if you have SSH to something, you can manage it. Simple.

Managing Vagrant Instances with Ansible
I will be using VirtualBox, and if you have got VirtualBox too we can get started!
If you look at my github repo you will Vagrantfile and it has lines like;

Vagrant.configure(“2”) do |config|
servers=[
{
:hostname => “ansible-control”,
:box => “bento/ubuntu-18.04”,
:ip => “192.168.56.100”,
:ssh_port => ‘2215’
},
{
:hostname => “db01”,
:box => “bento/ubuntu-18.04”,
:ip => “192.168.56.101”,
:ssh_port => ‘2210’
}

That file will provide us instances in Vagrant loop with their ports and IP addresses as you can see, IP addresses will be started from 192.168.56.100 till 104. And their image for OS will be Ubuntu version 18.04.

If you have got Vagrant in your local machine, lets get started!
Here is my lab right now and there is nothing, after run “vagrant up” everything will be started!

First lines of output after “vagrant up” should be like:
Bringing machine ‘ansible-control’ up with ‘virtualbox’ provider…
Bringing machine ‘db01’ up with ‘virtualbox’ provider…
Bringing machine ‘web01’ up with ‘virtualbox’ provider…
Bringing machine ‘web02’ up with ‘virtualbox’ provider…
Bringing machine ‘loadbalancer’ up with ‘virtualbox’ provider…

After some minutes everything will be done, after that we will connect to “ansible-control” machine for managing the other machines. As you can see i am using Windows PowerShell but when we SSH to “ansible-control” we will do everything in Linux way. Lets dive into machine and setup Ansible.

Now we are in a Ubuntu and Ansible machine, lets have Ansible with just 2 lines word.

Now try to ping other machines such as db01, web01… That might not work but give it a chance lol.

Looks our machine dont know these machines, its time to give their IP and names to /etc/hosts file. If you go to /vagrant directory you will see our resources and host files. Lets send that hosts_file to /etc/hosts and after that step our Ansible machine will know the targets.

Looks good! Time to do some Ansible things! Go to first_part and try some of Bash files, they have one line Ansible commands. As a first example lets ping the machines again but with Ansible now. ping.sh’s content is:
ansible -i hosts all -m ping That means “use ping (-m)module for all hosts”.
Hosts file has name our instances, it is inventory file for us and shows Ansible target with that.

But we forgat to do SSH things, if we type yes we will have error. Lets match instances with SSH try that again.
We should create key and copy them to instances. Dont forget default root username and password is vagrant, set everything as default. Just press enter for that.

I think there is nothing to stop us for pinging our instances! Run ping.sh and time to try again.
Look we did something successfuly!

In second example we will have a file with file module, we will declare the state of file and Ansible will make that for us.
That one line Ansible task work like “use file module and run ad hoc command, path of file is … and make it with touch”.

Now go inside one of machine and take a look of the file with its file at /tmp directory.

It is empty but exists!
Thanks for reading, you can find more examples in my github repo;
erdenayates/managing-vagrant-with-ansible (github.com)

That is all for this post, we will be talking about second part of the repo.

While i was having informations with these 2 good tools, i was watching DevOps Journey and Percy Grunwald from TopTechSkills Youtube channels. Thanks a lot them!

--

--

DevOps with Erdenay
DevOps with Erdenay

No responses yet