How to upload a vagrant file while provisioning a server
I was recently introduced to vagrant and i found it very interesting. I picked up a task that which is creating virtual machines using vagrant, setting up ( web server running on nginx and load balancing using haproxy) with ansible configuration tool. This article assumes you have a prior knowledge of provision server using vagrant. I will be writing more articles for newbies on vagrant. Please do check the next step section below for more details
I want to make the configuration as simple as possible and upload the files once the server is provisioned. The files includes inventory.ini and bootstrap_setup.sh. The vagrant file below shows how to upload the files during the process of bootstrapping the virtual machine (VM). Since this is my first time of working on vagrant, i had to do some research on how this can be achieved. This article explains the content of my vagrant file.
Below is a copy of the vagrant file
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
#Load Balancer (Machine 1)
config.vm.define "lb_mgt" do |lb|
lb.vm.box = "ubuntu/trusty64"
lb.vm.box_url = "trusty-server-cloudimg-amd64-vagrant-disk1.box"
lb.vm.hostname= "lb"
lb.vm.network "private_network", ip:"192.168.57.3"
lb.vm.network "forwarded_port", guest: 80, host: 8080
lb.vm.provider "virtualbox" do |vb|
vb.memory = 1024
end
lb.vm.provision "file", source: "inventory.ini", destination: "/home/vagrant/inventory.ini"
lb.vm.provision :shell, path: "bootstrap_setup.sh"
end
end
Source is the location of where the source file resides, which will be uploaded while destination is the location of where the file will be placed. Once the the commands vagrant up is executed, it will start to bootstrap the virtual machine with hostname lb. A copy of the inventory.ini file will be moved to /home/vagrant/inventory.ini and the bootstrap.sh script will kick in.
bootstrap_setup.sh contains all the installation file for ansible, this will install ansible once the server is been provisioned. A copy of the file on inventory.ini while is already on the server will be moved to the ansible directory on /etc/ansible/inventory.ini
bootstrap_setup.sh
#!/usr/bin/env bash
sudo apt-get -y install software-properties-common
sudo apt-add-repository -y ppa:ansible/ansible
sudo apt-get -y update
sudo apt-get -y install ansible
#copy inventory.ini file
sudo cp /home/vagrant/inventory.ini /etc/ansible/inventory.ini
# configure hosts file for our internal network defined by Vagrantfile
sudo cat >> /etc/hosts <<EOL
#vagrant environment nodes
192.168.57.3 lb
192.168.57.41 webServer1
192.168.57.42 webServer2
EOL
inventory.ini
This contains the inventory file for ansible setup. lb represent load balancer and the ip address is already configured in the host file.
[lb]
lb
Next Step
I will continue with the post by writing a complete article which will contain the continuation of the task. The following articles will include
- Article 1: Explain vagrant, virtual box, setup ansible, installation and configuration of softwares (haproxy and ansible)
- Article 2. Testing
Keep coding!
Kindly ensure to drop your comments.