Vagrant Provisioning


After completing Vagrant Networking we will continue with Vagrant Provisioning.

In this section we will see how to automate the first configuration and installation of software on our virtual machines. Remember that we before installed Apache Web Server. Now lets do it by provisioning in an automated way. To do this we will write a simple script. We only need “apt-get update” to update software lists and after that “apt-get -y install apache2“.  Our final shell script will be like the one below:

sudo apt-get update
sudo apt-get -y install apache2

Create a file under your vagrant project folder with name provision.sh and write the details above. Now lets check the contents of the Vagrantfile:

VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
   config.vm.define "box1" do |box1|
      box1.vm.box = "ubuntu/trusty64"
      box1.vm.network "forwarded_port", guest: 80, host: 8080
      box1.vm.provision "shell", path: "provision.sh"
   end
end

In the Vagrantfile we are creating an Ubuntu 14.04 virtual machine. There is also port forwarding defined and lastly our shell code is defined. Now run “vagrant up” and see what happens:  You will see the “apt-get update" output and after that the installation of apache. Check if it is really installed and running.

$ vagrant ssh
[email protected]:~$ service apache2 status
 * apache2 is running

Lastly from your host machine check the apache server gives and output by checking URL http://localhost:8080

Install MySQL Server and PHP:

Now lets make the scenario a bit complicated. We will install Mysql server and php5 and the php5-mysql package. This will create a fully running LAMP (Linux-Apache-Mysqlserver-Php) server for us.

sudo apt-get update
echo mysql-server mysql-server/root_password password mypass | sudo debconf-set-selections
echo mysql-server mysql-server/root_password_again password mypass | sudo debconf-set-selections

sudo apt-get -y install apache2 mysql-server php5 php5-mysql

Above we added two new lines: line 2 and 3. These lines are needed to answer the question that mysql-server asks for the root password. We are setting the question answers so the installation will complete without any problem. The root password for mysql-server will be mypass. In the last line we added “mysql-server, php5 and php5-mysql” details. Change the provision.sh file accordingly. Run “vagrant destroy --force” to destroy the previously created virtual machine. Run again “vagrant up” and see what is happening.

Now lets see if we can connect to mysql server and if php5 is installed:

$ vagrant ssh
[email protected]:~$ service mysql statusmysql start/running, process 5680
[email protected]:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 42
Server version: 5.5.38-0ubuntu0.14.04.1 (Ubuntu)Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> exit
Bye
[email protected]:~$ sudo apt-cache policy php5
php5:
Installed: 5.5.9+dfsg-1ubuntu4.4
Candidate: 5.5.9+dfsg-1ubuntu4.4
Version table:
*** 5.5.9+dfsg-1ubuntu4.4 0
500 http://archive.ubuntu.com/ubuntu/ trusty-updates/main amd64 Packages
500 http://security.ubuntu.com/ubuntu/ trusty-security/main amd64 Packages
100 /var/lib/dpkg/status
5.5.9+dfsg-1ubuntu4 0
500 http://archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages

Above:
1- We checked if mysql service is running with “service mysql status
2- With “mysql -u root -p” we tried to connect the mysql server and entered password and saw that it is working
3- With “sudo apt-cache policy php5” we checked if php5 installation is successful.

We have completed “Vagrant Provisioning” here. As a final notice this section only covers provisioning with shell script. There are also other possibilities like Puppet and Chef.

Links: https://docs.vagrantup.com/v2/getting-started/provisioning.html

Next Post: Vagrant ? Create a Puppet Server and Agent