Infrastructure as code
This workshop can be done on your own, or in groups of 2-3 people.
- Part 1 - Using Vagrant with Shell Provisioner
- Part 2 - GitLab installation using Vagrant and Ansible Provisioner
- Install VirtualBox - https://www.virtualbox.org/wiki/Downloads
- Install Vagrant on your computer - https://www.vagrantup.com/downloads.html
- (Optional) On Windows, ensure that Hyper-V is disabled:
- Open a new PowerShell
- Run the following command:
Disable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-All
- Download the
centos/7
Vagrant box for the Virtualbox provider, run:
vagrant box add centos/7
It will output:
==> box: Loading metadata for box 'centos/7'
box: URL: https://vagrantcloud.com/centos/7
This box can work with multiple providers! The providers that it
can work with are listed below. Please review the list and choose
the provider you will be working with.
1) hyperv
2) libvirt
3) virtualbox
4) vmware_desktop
Enter your choice: 3
- Clone the lab repository to your computer:
git clone https://github.com/adaltas/ece-devops-2020-fall.git
The next step once you’ve installed Vagrant is to create a Vagrantfile
and customize it to suit your needs. This is covered in detail in the Vagrant documentation, and for this lab we will use a prepared Vagrantfile
.
Go to the modules/3-infrustructure-as-code/assets/part-1
directory and take a look at the Vagrantfile
:
cd ece-devops-2020-fall/modules/3-infrustructure-as-code/assets/part-1
Run the command:
vagrant up
Try also other Vagrant commands that manage the VMs:
# will check VMs status
vagrant status
# stop the VMs
vagrant halt
# will destroy VMs
vagrant destroy
- To enter inside the VM via SSH:
vagrant ssh centos_server
It will open a session in the VM and you can run any bash commands being inside the Linux VM (like ls
, pwd
, etc.)
- Open VirtualBox (or VMware) and check the installed virtual machine.
Find more instructions here.
- To configure the
/etc/hosts
file replace "Start provisioning" section in theVagrantfile
with this:
# Start provisioning
config.vm.provision "shell",
inline: "echo '127.0.0.1 mydomain-1.local' >> /etc/hosts"
Then run:
vagrant provision
Note! You need to run it with your laptop OS terminal, not being inside the VM via SSH.
- To print a current date into the
/etc/vagrant_provisioned_at
file replace in theVagrantfile
with this:
# Start provisioning
$script = <<-SCRIPT
echo I am provisioning...
date > /etc/vagrant_provisioned_at
SCRIPT
config.vm.provision "shell", inline: $script
Then run:
vagrant provision
We will install Gitlab on CentOS as described in the official documentation. You can try repeat those steps manually one after another on the VM configured in the part 1 of the lab. Usually, when we are installing first time a new software on a testing environment, we do it manually to test each step and then, after clarifying all the installation process we automatize it using tools like Vagrant and Ansible.
We will use ansible_local
provisioner what will install Ansible on CentOS 7 Linux distribution virtual machine by Vagrant. So, you don't need Ansible on your host OS!
Go to the modules/3-infrustructure-as-code/assets/part-2
directory and take a look at the Vagrantfile
and at the YAML files playbooks/run.yml
. To have more information how the Vagrantfile
is configured follow this Vagrant Guide
In the assets/part-2
directory, you will find:
- A
Vagrantfile
that defines the VMs to be managed by Vagrant (1 CentOS 7 VM namedgitlab_server
in our case) - A
playbooks/
directory that contains Ansible playbooks to install GitLab and run health checks
Run the command:
vagrant up
It will take 5-10 min to install all the necessary software including required packages, GitLab instance and databases.
To test the installation of GitLab you can just open a URL in a browser and make sure it answers with any GitLab page (this is a step 3 of the GitLab installation doc).
So, open in your browser the URL - http://20.20.20.2
Once it is open the login page you can login and explore GitLab:
- Choose a password
- Login as the user
root
using the password
If you need to change something in our installation, you have to modify your playbooks, upload them on the VMs and run provisioning:
- Update the playbooks on the VM using
vagrant upload
:
vagrant upload playbooks /vagrant/playbooks gitlab_server
- Rerun provisioning with the command:
vagrant provision