Building an On-Prem IaC Workflow
Creating an IaC Pipeline
Now that we have learned the basics about on-prem IaC, it is time to put our knowledge to the test! Start the machine attached to this task by pressing the green Start Machine button. The machine will start in split-view. In case it's not showing up, you can press the blue Show Split View button at the top-right of the page.
Once the machine has started, you can navigate to the /home/ubuntu/iac/
directory. All the scripts that we will be using today can be found in this directory.
Vagrantfile
Let's start by having a look at the Vagrantfile:
In this Vagrantfile, we can see that two machines will be provisioned.
DB Server
The first machine that will be provisioned is the dbserver
. Working through the lines of code, we can see that the machine will be added to a local network and receive the IP of 172.20.128.3
. We can also see that the provision directory will be mounted as a share. Lastly, using Docker as the provider, the mysql
image will be booted and the mysql password will be configured to be mysecretpasswd
.
Web Server
The second machine that will be provisioned is the webserver
. Similar to the dbserver
machine, it will be connected to the network and use Docker as its provider. However, there are some slight differences. Firstly, the webserver will expose SSH. Since we are using Docker, we have to alter some of the default Vagrant configurations to allow Vagrant to connect via SSH. This includes changing the username and the private key that will be used for the connection. Secondly, we can see that an Ansible playbook will be executed on the container by looking at the following line:
Let's have a look and see what this Ansible playbook will do.
Ansible Playbook
Let's start by reviewing the web-playbook.yml
file:
This is a simple Ansible script that indicates that the webapp role will be provisioned on the host.
To better understand what the webapp role will entail, we can start by reviewing the ~/iac/provision/roles/webapp/tasks/main.yaml
file:
This shows us that there will be two main portions to the Ansible provisioning. At this point, it is worth taking a look as well at the default values in the ~/iac/provision/roles/webapp/defaults/main.yml
file:
We will get back to these variables in a bit, but keep them in mind.
DB Setup
Let's have a look at the db-setup.yml
file:
From the script, we can see that 7 tasks will be performed. Reading through these tasks, we can see that a temporary folder will be created where SQL scripts will be pushed to and then executed against the database host.
Let's have a look at how Ansible would inject those variables from before. Have a look at the Create DB
task's shell command:
As you can see, the three variables of db_user
, db_password
, and db_host
will be injected using either the values for the default file, or the overwritten values, if they exist.
Ansible allows us to take this a step further. Let's have a look at the actual createdb.sql
file:
As we can see, these variables are even injected into the file templates that will be used. This allows us to control the variables that will be used from a single, centralised location. When we change the user or password that will be used to connect to the database, we can change this in a single location, and it will propagate throughout all provisioning steps for the role.
Web Setup
Lastly, let's have a look at the app-setup.yml
file:
This file only has two tasks. The first copies the artefacts required for the web application, and the second copies the web application file as a template. A template copy is performed to ensure that the variables, such as the database connection string, are injected into the script as well.
We will not do a deep dive into the rest of the files that will be used for provisioning, however, it is recommended that you have a look at these files to gain a better understanding on what exactly we are provisioning.
Running the IaC Pipeline
Now that we have an understanding of our pipeline, it is time to start it! Let's start our pipeline and the provisioning using vagrant up
from the iac
directory. The pipeline will take a while to boot, but pay attention to what is happening.
While you may see some red on the terminal when the Ansible provisioning step is running, as long as these lines only indicate warnings and not an error, the provisioning will complete as expected.
Once our pipeline has provisioned the machines, we can verify that they are running using the docker ps
command:
If this is running, we can start our web application using the following command:
vagrant docker-exec -it webserver -- python3 /app/app.py
Once loaded, you can navigate to the web application using the target machines's browser (http://172.20.128.2/):
Last updated