DevOps

Ansible Provisioning: Smarter and Effortless way of Provisioning

Last updated on Jun 17,2019 2.6K Views

Kalgi ShahKalgi 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...

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.

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.

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.

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.

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.

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.

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.

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.

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.

Fig. 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.

Fig. 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.