When managing our applications, part of the essential things we consider is the infrastructure, meaning, and resources that we need to help us handle the scaling of our applications. In recent times, people have tended to move to cloud infrastructure, that is they deploy their applications to the cloud, leaving the provision of hardware and computational resources to the cloud providers to handle.
In this article, we will focus on how we can use code to optimize the provisioning of our infrastructure in the cloud. Let’s get right into it.
What is Infrastructure as Code?
Infrastructure as Code can be described as the automation of infrastructure needed for the end-to-end deployment of an application.
It is also referred to as the technique that is used to define your infrastructure provisioning and configuration the same way you handle application code.
Why is Infrastructure as Code Important
The problem IaC aims to solve is the reduction of time and effort needed to manage the deployment process of an application.
For instance, you have an application that needs to scale up, and this application needs one hundred servers to run smoothly. Creating each server individually will be an inefficient approach, plus it will increase the time at which it will take your application to scale.
Like every other introduced concept in tech. there are also the benefits that come with it. Here are some of the benefits of IaC.
- Increase in deployment speed and efficiency: With IaC implemented, there will be an increase in software deployment and speed, because you only need to run the code that automates the infrastructure provisioning rather than provisioning and configuring these infrastructures personally.
- Improved software development life cycle: Generally, adopting the concept of IaC leads to a better software development life cycle. One of the key metrics of SDLC is the reduction in production time, implementing IaC helps achieve this. IaC takes over the process of infrastructure management operations, which helps reduce the time spent in building products both in development and production environments,
- Improved infrastructure management: IaC helps improve the overall structure of infrastructure management. It achieves this by
- Reduction of human error: IaC reduces human errors. With code in place of humans handling the operations of infrastructure configuration and deployment. It reduces the errors humans are more likely to make.
Examples of Infrastructure as Code Tools
There are different tools for implementing infrastructure as code. These tools are used to carry out different tasks in infrastructure provisioning, management, and deployment.
Here are some of the Infrastructure as Code we have.
- AWS CloudFormation, and more.
Note: No one tool is used to handle the different aspects of infrastructure management, rather these tools are used together to facilitate much better infrastructure management.
What is Ansible
Ansible is an open-source infrastructure as a code tool supported by RedHat. it is used to handle multiple server configurations and management from one point. It helps automate the configuration of multiple servers at once, rather than connecting to an individual server and then carrying out your desired task.
What makes Ansible Stand Out?
Here are some points that make Ansible stand out from its alternatives (Chef, Puppet, Salt, etc.):
- Ansible is often recognized as a simpler beginner-friendly tool
- Its configuration files are written in YAML, which is easier to understand
- It is lightweight and does not require many computing resources
- Compared to its alternatives, to make changes on other servers, with Ansible you only need to SSH into those servers to make changes.
Writing your first Ansible Script
In this section, we are going to go over how to install and set up an Ansible on Linux, then we will write a script to automate the installation of Nginx on two of the servers we will provision.
First of all, to get started, we need to provision two servers we are going to work with. To do that, we are going to need a cloud provider, we are going to use Linode.
If you do not have an account already, head on to linode.com to create one. After creating an account, create two Linode instances.
Now that our Linode instances have been created, we need to install Ansible on our local machine.
To install Ansible on our Linux machine, we need to open our terminal and enter this command:
To check if Ansible has been successfully installed, enter this command into your terminal
It should return the version of Ansible you have installed, if not, Ansible was not successfully installed.
Next, we need to change our current directory to /etc/ansible. In this directory, we should see two files and one folder in there, namely:
Next, we need to edit the host file. The host file is also known as our Inventory. We need to add our Linux servers’ IP addresses to the inventory, alongside their login credentials. That way, our Ansible script knows how to log in to our servers and make the configurations we need it to make.
Now, while in our host file, let us add our Linux servers IP to a group. We will go down to the end of our file to make the changes like so:
Next, we need to define our login credentials, so Ansible can log in to our servers. We can do this by adding this group below the linux we defined earlier.
Now, we save and exit the hosts file. To check if we have made a connection to our servers, we must enter this command into our terminal.
We should get a response like this.
We can do more than ping our servers, we can also update the repositories on our servers with this command
You can check out more ansible host flags in their documentation.
Next, we will not write our first Ansible script, otherwise known as a Playbook.
Ansible playbooks are the configuration files written in YAML, in which we specify what tasks we want to run on our servers.
These playbooks contain plays or tasks which we define that will get executed on our servers.
Let’s create our Ansible Playbook. Firstly, we need to create a YAML file called install_nginx.yml. In our terminal, we need to type this in:
Now add this piece of code into the file...
In our Playbook here, we are starting with 3 hyphens indicating that the type of file we are working with is a YAML file.
Next, we are specifying our host as linux which we created in our hosts file earlier on, followed by our task.
In our task, we are giving it the name install the latest version of nginx, followed by the module which we are using to perform our installation, here we using the apt module because our servers are running Ubuntu.
Next, we specify the name of the package we want to install, which is nginx followed by the state of the package, which is latest in our case.
Finally, save and close the file. To run our Ansible Playbook, all we have to do is run this command in our terminal.
To confirm our Playbook was executed without any errors, we should see this output:
There you have it, we just wrote our first Ansible Playbook. There are different ways to write Ansible Playbooks, we only focused on making our Playbook install nginx on our servers.
We focused on the key points surrounding Infrastructure as Code on this one. We also went through a brief introduction to Ansible and wrote our first Playbook.
Thank you for sticking around till the end. Until next time, happy coding!