AWS Global Infrastructure

DevOps

Topics Covered
  • DevOps (74 Blogs)
  • Mastering Git and GitHub (3 Blogs)
  • Docker (9 Blogs)
  • DevOps Engineer Masters Program (18 Blogs)
SEE MORE

Ansible Provisioning: Smarter and Effortless way of Provisioning

Last updated on Jun 17,2019 8.8K Views

Kalgi Shah
Kalgi Shah works at Edureka as Research Analyst. Always curious about the... Kalgi Shah works at Edureka as Research Analyst. Always curious about the wonders of technology. Fields like Artificial Intelligence, DevOps, Data Analytics, Kubernetes attract...
4 / 8 Blog from Ansible

The first step in automating any operational life cycle of the application is automating the provisioning of the infrastructure. Provisioning is an annoying process of setting-up configurations, allocating memory, disk space, etc., before actually using them. Let’s see how Ansible Provisioning makes this simpler and faster.

Most of the large scale deployments require you to have an identical environment setup on multiple systems. So, how do you do that? Execute the same set of commands on a hundred systems manually?? Nahh.. That’s too old school. This is something machines are good at, not humans. What if I told you this long and boring process could be done by just executing a single Ansible playbook? That’s what we’re going to talk about in this blog – Ansible Provisioning.

Topics covered are:

If you wish to master DevOps, this course should be your go-to option.

What is Ansible?

DevOps is a culture where automation is given a really high priority. When there can be tools for automating the deployment and the testing process, why not have tools for configuration management and provisioning. There are a million ways to get smarter and using Ansible is one of them. It is one of the most widely used tools.

Ansible uses YAML based playbooks that can even be understood by a beginner. It uses an agentless architecture to communicate with its clients which does not involve any third party tool to be installed on the client’s machine, and instead uses SSH-based connections. Ansible being an automation tool, let’s find out what can it automate. 

What can Ansible Automate?

Name a stage in the development cycle and Ansible will help you with it, be it configuration management, provisioning, orchestration, continuous delivery, security or even application deployment. Ansible unites the development cycle workflow into a single agent less automation platform.

  1. Provisioning: Creating a suitable environment for the application/ software to live is necessary. Ansible provides a way to automate the environment created for the application’s existence.
  2. Configuration Management: Perform a wide variety of configuration tasks, such as start/ stop services, change the configuration of a system, device or an application, etc.
  3. Application Deployment: Automate the defining of deployment with Ansible and manage the deployment using Ansible tower. This makes the entire application cycle from production to deployment efficient and manageable.
  4. Continuous Delivery: Creating and managing a continuous integration/ continuous delivery pipeline can get cumbersome. That’s where Ansible steps in and makes the developer’s lives easier. 
  5. Security and compliance: Working with the projects, always sets boundaries and is integrated with the company’s security policies. Having security policies integrated automatically with the deployment can make complying with policies easier. 
  6. Orchestration: An entire project is a collection of many different instances having a different configuration. Ansible merges and manages these different instances as a whole.

Need for Ansible Provisioning

As mentioned earlier, the first step to automating the operational life cycle of the applications is getting the environment ready, i.e, provisioning. Large deployments require multiple hosts having the exact same configurations. After provisioning one host, what are the chances that you’ll get the next 10 hosts to have the exact same configurations if done manually? Also how much time will you spend doing the same repetitive task? This is where Ansible comes to our service. You can provision hundreds of hosts just by executing a single playbook. Magic? Haha! No, just evolution in automation.

Demo: Create a LAMP Stack and Deploy a Webpage

Suppose you’re trying to deploy a website on 30 systems, every website deployment will require a base OS, web-server, Database, and PHP. We use ansible playbook to install these pre-requisites on all 30 systems at once.

In this Ansible provisioning demo, I’m going to show you how to provision the website hosting environment using Ansible. We install the LAMP (Linux, Apache, MySQL, and PHP) stack and then deploy a website.

For this demo, I’ve used a Linux VirtualBox having Ubuntu version 17.04. I’ve used two virtual machines, one as my server where Ansible is installed and the other machine acts as my remote host. Let’s start with setting Ansible on the server.

I’ve created a simple static webpage saved in a folder index which has two files, index.html, and style.css.

index.html:

<html>
<head>
<title>Website using HTML and CSS</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="">Breeds</a></li>
<li> <a href="">Adoption</a></li>
<li> <a href="">Contact</a></li>
</ul>
</div>
<div class="tagline">
<h1>Share your home with a cute dog!</h1>
<div class="adopt">
<a href="" class="bttn">Adopt Now</a>
</div>
</div>
</header>
</body>
</html>

style.css

*
{
margin:0;
padding:0;
}

header {
background-image: linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),url('puppie.jpg');
height:100vh;
background-size:cover;
background-position:center;
}

.nav-menu {
float: right;
list-style:none;
margin-top:30px;
}

.nav-menu li {
display: inline-block;
}

.nav-menu li a {
color:turquoise;
text-decoration: none;
padding:5px 20px;
font-family:"Verdana", "sans-serif";
font-size:20px;
}

.homebtn a {
border: 1px solid grey;
background-color:white;
}

.nav-menu li a:hover {
border:1px solid grey;
background-color: white;
}

.tagline {
position: absolute;
width:1200px; 
margin-left:0;
margin-top:0;
}

h1 {
color:white;
font-size:50px;
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: 22px;
text-decoration:none;
}

.adopt a:hover {
background-color: burlywood;
}

Step 1: Execute the following commands to update repositories, add the required repositories and configure PPA on your machine for Ansible installation: 

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible

Press enter to accept the PPA addition and then finally install Ansible after updating the repositories.

$ sudo apt-get update
$ sudo apt-get install ansible

Step 2: Go to your server’s /etc/hosts file and add the hostname and the IP address of the host.

etc hosts - Ansible provisioning - EdurekaFig. 1 – Add Remote hosts to /etc/hosts File – Ansible Provisioning

Step 3: As Ansible works on the agentless architecture of using SSH to communicate with its hosts, set-up the ssh keys. Basically, we have one server and one host. We control the host with our server and hence we create a public ssh-key on the server and copy it in the host’s machine. Execute the following command on the server:

$ ssh-keygen 

You’ll be prompted to enter the filename where you’d like to save your key and also prompt you for creating a password for accessing the generated key which is optional. By default, the public key gets saved in .ssh/id_rsa.pub file and the private key gets saved in .ssh/id_rsa.

ssh-key - Ansible provisioning - EdurekaFig. 2 – Create ssh Key – Ansible Provisioning

Now this generated key needs to be present in your host. Copying the key into the host can be done in two ways, either manually copy into the host or use the ssh-copy-id command. In this case, I’m going to copy it using the ssh-copy-id root@IP_of_host command. 

$ ssh-copy-id root@192.168.56.104

Note- Make sure you’re able to ssh into your host before executing this command.

Step 4: Configure Ansible hosts. Go to /etc/ansible/hosts file and add the hostname. This will vary according to the number of hosts and servers you have. You can also have more than one server here. 

ansible hosts-Ansible provisioning - EdurekaFig. 3 – Add Remote Hosts in the Inventory File – Ansible Provisioning 

Step 5: Check if your hosts are ready. Execute this command you should get a similar output.

$ ansible -m ping all

ping-host - Ansible provisioning - EdurekaFig. 4 – Check Remote Host’s Status – Ansible Provisioning

Step 6: Now our Ansible is ready, let’s make the environment ready to deploy a website. We’re going to use a single Ansible playbook to install Apache, MySql, and PHP. Let’s have a look at it. 

Note: If you’re a beginner, have a look at this blog which explains how to write a playbook.

---
# Setup LAMP Stack
-  hosts: host1
   tasks:

      -  name: Add ppa repository
         become: yes
         apt_repository: repo=ppa:ondrej/php

      -  name: Install lamp stack
         become: yes
         apt:
            pkg:
              - apache2   
              - mysql-server
              - php7.0
              - php7.0-mysql   
            state: present
            update cache: yes

      -  name: start apache server
          become: yes
          service: 
              name: apache2
               state: started
                enabled: yes

      -  name: start mysql service
          become: yes
          services:
             name: mysql
             state: started
             enabled: yes

      -  name:  create target directory
          file: path=/var/www/html state=directory mode=0755

      - name:  deploy index.html
         became: yes
          copy: 
              src: /etc/ansible/index/index.html
              dest: var/www/html/index/index.html

Here as you can see, we have 6 tasks, each task performs a specific function.

  • The first task adds the repository required to install MySQL and PHP.
  • The second task installs apache2, MySQL-server, PHP, and PHP-MySQL.
  • The third and fourth task starts the Apache and MySQL service.
  • The fifth task creates a target directory in the host machine and
  • Finally, the sixth task executes the index.html file, it picks up the file from the server machine and copies it into the host machine.

Execute this playbook with the following command:

$ ansible-playbook lamp.yml -K

The lines become: yes in the playbook tells that it needs to execute as root and hence when you execute the command, it’ll prompt for sudo password.

root-password - Ansible provisioning - EdurekaFig. 5 – Execute Ansible Playbook – Ansible Provisioning

Now you can go into the host machine and check if the website has been hosted.

hosted-website - Ansible provisioning - EdurekaFig. 6 – Host the Website on Localhost – Ansible Provisioning

Now that is a webpage that would be deployed in all the hosts that interact with the server(in our case, we only had one host) but the same would be possible even for 100 remote hosts.

This brings us to the end of Ansible Provisioning blog. If you find this article helpful, check out the DevOps course offered by Edureka. It covers all the tools that has made the IT industry smarter.

Got a question for us? Please post it on Edureka Community and we will get back to you.

Upcoming Batches For DevOps Certification Training Course
Course NameDateDetails
DevOps Certification Training Course

Class Starts on 23rd March,2024

23rd March

SAT&SUN (Weekend Batch)
View Details
DevOps Certification Training Course

Class Starts on 13th April,2024

13th April

SAT&SUN (Weekend Batch)
View Details
DevOps Certification Training Course

Class Starts on 15th April,2024

15th April

MON-FRI (Weekday Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Ansible Provisioning: Smarter and Effortless way of Provisioning

edureka.co