How to upload a vagrant file while provision a server

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 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| = "ubuntu/trusty64"
lb.vm.box_url = ""
lb.vm.hostname= "lb" "private_network", ip:"" "forwarded_port", guest: 80, host: 8080
lb.vm.provider "virtualbox" do |vb|
vb.memory = 1024

lb.vm.provision "file", source: "inventory.ini", destination: "/home/vagrant/inventory.ini"
lb.vm.provision :shell, path: ""

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 script will kick in. 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

#!/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 lb webServer1 webServer2


This contains the inventory file for ansible setup. lb represent load balancer and the ip address is already configured in the host file.


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

  1. Article 1: Explain vagrant, virtual box, setup ansible, installation and configuration of softwares (haproxy and ansible)
  2. Article 2. Testing

Keep coding!

Kindly ensure to drop your comments.

Original post (opens in new tab)