When it’s about the automation of applications, provisioning of infrastructure is a must. Provisioning becomes very annoying when developers need to configure resources, allocate memory, and many other tasks manually. Therefore Ansible came into existence. In this post, we will see Ansible provisioning with the help of a demo.
This post covers:
- What is Ansible?
- Why do we need Ansible Provisioning?
- Where can we use Ansible Automation?
- Hands-on: Provision the Environment to Deploy a Website
- Conclusion
What is Ansible?
Ansible is an open-source automation tool for IT. It helps to deploy applications, update workstations, servers, cloud provisioning, and configuration management. It makes the job of a system administrator easy. It is easy to deploy because it is agentless and does not require any additional infrastructure for security. As Ansible is an automation tool, it needs instructions to accomplish each task. It uses YAML based easy to understand playbooks.
Why do we need Ansible Provisioning?
While deploying any application, it is always necessary to automate its operational lifecycle via provisioning. Large companies and organizations have their own business needs. Usually, these needs lead to deploy heavy applications on a larger scale. There can be a scenario where applications need 20 hosts for deployment with the same configuration. Users are not going to configure those 20 hosts manually on each device. Instead, they can provision one host by using Ansible, and all others will be provisioned automatically having the same configuration.
Let’s see where can Ansible Automation be used?
Where can we use Ansible Automation?
Ansible is a powerful tool that helps in every aspect of the development cycle, from configuration management to provisioning, orchestration, application deployment, and many other things. Let’s discuss in brief what Ansible can automate.
- Configuration Management: Ansible is the best fit for configuration management. It can perform numerous tasks such as start or stop the service, change the system configuration, etc.
- Application Deployment: Ansible helps to define the deployment, and Ansible tower manages the whole deployment process. This way, the entire application deployment becomes easy, cost-effective, and manageable.
- Provisioning: Ansible creates a suitable environment for the deployment of any application or software by provisioning them.
- Continuous Delivery: It is not easy to deal with a continuous integration/ continuous delivery pipeline. This is where ansible makes the developer’s job easy.
- Orchestration: A single project consists of many small programs or instructions with different instances configuration. Ansible smoothly merges and manages these configurations and instances as a complete project.
- Security and Compliance: While working on various projects, it is always necessary to adhere to the security policies set by the company. Ansible integrates with the security policies automatically.
Check out: Ansible Configuration Management Tools
Hands-on: Provision the Environment to Deploy a Website
Consider a scenario where you need to deploy any website for 20 systems. . It requires a web server, base operating system (Windows, Linux, Mac), PHP, and a database to deploy any website. You are not going to make all twenty systems ready for deployment manually. Rather, you can use Ansible playbooks to configure everything at once on all twenty systems. This reduces the annoying process of confusing each system manually as Ansible takes care of the provisioning.
In this demonstration, we will use a basic static website to provision the hosting environment of a website on a Linux (Ubuntu) Virtual Machine.
The image shown below is a simple static website created in HTML and CSS.
Check out the Code used for this website.
HTML Code
<html> <head> <title>Demo Website</title> <link href="style.css" rel="stylesheet" type="text/css"> </head> <body> <header> <div class="top"> <ul class="nav-menu"> <li class="homebtn"> <a href="">Home</a></li> <li> <a href="">About</a></li> <li> <a href="">Bookings</a></li> <li> <a href="">Gallery</a></li> <li> <a href="">Contact</a></li> </ul> </div> <div class="tagline"> <h1>Welcome to the Nature</h1> <div class="adopt"> <a href="" class="bttn">Book Now</a> </div> </div> </header> </body> </html>
CSS Code
* { margin:0; padding:0; } header { background-image: linear-gradient(rgba(24, 179, 153, 0.5), rgba(0,0,0,0.5)),url('nature.jpeg'); height:100vh; background-size:cover; background-position:center; } .nav-menu { float: left; list-style:none; margin-top:30px; } .nav-menu li { display: inline-block; } .nav-menu li a { color:rgb(255, 255, 255); text-decoration: none; padding:5px 20px; font-family:"Verdana", "sans-serif"; font-size:20px; } .homebtn a { border: 1px solid black; background-color:rgb(5, 0, 0); } .nav-menu li a:hover { border:1px solid black; background-color: rgb(0, 0, 0); } .tagline { position: absolute; width:1200px; margin-left:0; margin-top:0; } h1 { color:white; font-size:48px; font-family:"Verdana", "sans-serif"; text-align:center; margin-top:275px; } .adopt { margin-top:30px; margin-left:540px; } .bttn { border: 1px solid white; padding:10px 30px; color:yellow; font-family:"Verdana", "sans-serif"; font-size: 20px; text-decoration:none; } .adopt a:hover { background-color: rgb(5, 5, 5); }
You can use these codes given above to perform this demo.
Also Check: Our blog post on Advanced Ansible Tutorial.
System Requirement
- We have used a Virtual Box with Linux (Ubuntu) installed inside it. Two virtual machines have been used for this demo:
- The first virtual machine is used to install Ansible and acts as a server.
- The second Virtual machine is used for remote hosting.
- LAMP Stack is used for the environment and to deploy a website.
Steps to Provision the environment
Step 1) Make sure you have installed Ubuntu in your system. The first step is to update the existing repositories, add required repositories and configure PPA for Ansible installation on your system.
Follow these commands listed below in the Terminal to perform step 1.
$ sudo apt-get update $ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
When you paste this command, make sure to accept the PPA by pressing enter. Now we will update repositories by following commands and then finally install Ansible.
$ sudo apt-get update $ sudo apt-get install ansible
Step 2) Now, you need to provide the hostname and the IP address of the host. Open your terminal. Now follow the command given below:
$ sudo nano /etc/hosts
This will open up an editor to provide the hostname and IP address. Do not forget to save when you provide the name and IP address for the host.
Step 3) Ansible is an agentless tool that uses the SSH method to communicate with hosts. So now we will set up the SSH key. Here, we are using one host and one server. The host is controlled by the server. So to do that, a public SSH key is created on the server and then copied to the host.
Follow the command listed below to generate an SSH-Key:
$ ssh-keygen
It will ask to enter the file name and passphrase. It is completely optional, so you can press enter to skip it, and it will generate an ssh-key.
Now, copy this key into the host machine with the help of a command given below:
$ ssh-copy-id root@192.168.56.104
Step 4) Next step is to configure the Ansible Host. Follow the commands given below:
$ sudo nano /etc/ansible/hosts
This will open up an editor to enter the hostname as shown in the image. Enter the hostname and do not forget to save it.
Step 5) Once your host is ready, follow this command given below to check the status of the host.
$ ansible -m ping all
You will get the output as shown in the image below:
This output means your Ansible is good to go.
Step 6) Next task is to make the environment ready for deploying the website. We are using a single Ansible playbook to install MySql, PHP, and Apache.
# Setup LAMP Stack - hosts: host tasks: - name: Add ppa repository become: yes apt_repository: repo=ppa:ondrej/php
# Adding repositories to install MySql and PHP in the system - name: Install lamp stack become: yes apt: pkg: - apache2 - mysql-server - php7.0 - php7.0-mysql state: present update cache: yes
# It is installing apache2, PHP, MySQL-server, and PHP-MySQL - name: start apache server become: yes service: name: apache2 state: started enabled: yes
# It starts the APACHE server - name: start mysql service become: yes services: name: mysql state: started enabled: yes # It starts the MySQL service.
- name: create target directory file: path=/var/www/html state=directory mode=0755
# It is creating target repositories in the host device. - name: deploy index.html became: yes copy: src: /etc/ansible/index/index.html dest: var/www/html/index/index.html
# It executes the index.html file and copy the file from server machine to host machine.
Now run the playbook using the command given below:
$ ansible-playbook lamp.yml -K
The moment you execute this command, it will ask for the sudo password because the lines become: yes in the playbook means that it needs to be executed as root and root requires a password.
You can check out the host machine to see if the website has been hosted, as shown below. In this scenario, we used only one host, but this website can be deployed in multiple hosts in the same manner.
This was a straightforward scenario to provision the environment to deploy a website.
Check out: Ansible vs Terraform
Conclusion
DevOps is very popular for automation purposes. There are a number of tools available for deployment and testing processes but rare for provisioning and managing configurations. Hence, Ansible provisioning is one of the best tools to automate the deployment and manage configurations in the market. It makes the tasks of developers and system administrators very easy.
Related/References
- [AZ-400] Roles And Responsibilities As An Azure DevOps Engineer
- [AZ-400] Microsoft Azure DevOps Certification Exam: Everything You Need To Know
- Check out the official Docs for Ansible Provisioning
- Check out Ansible for Beginners | Overview
Next Task For You
If you are considering in-depth learning about Ansible in the coming days, then do join our WAITLIST and don’t miss an opportunity to attend a free class and gain a plethora of insights on the basics of Ansible.
Leave a Reply