Automating Cloud Infrastructure with Ansible

By Kalapriya Kolappan, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud’s incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.

“You’re either the one that creates the automation or you’re getting automated,” Tom Preston-Werner

For the past few years, we have experienced many disruptions in business trends. This has led to build a constructive way for megatrends associated with emerging technologies and one such thing is ‘AUTOMATION’. On this note, welcome to the decade of automation. We want everything to be done on time. Despite doing all jobs manually, we want our daily routines to get automated. Hence, we want to sharpen our knowledge to avoid doing tasks in the nick of time. This could be achieved by Ansible efficient code which replaces all our day to day activities. Perhaps, we need to have a solution in our local environment to make our life easier. But where to begin?

In this article, we will articulate about the concepts of automation, the benefits of Ansible, and how Ansible influences automation in cloud computing, using Alibaba Cloud Elastic Compute Service (ECS).

Why Automation?

The world is moving towards automation. Automation with Ansible makes organizational operations easier while managing company-wide infrastructure. In this epoch of technology, we’ll mainly focus on a bunch of reasons for why we think automation is a critical part in all IT enhancements.

  1. Less error-prone: It is very common to mistype, misread, put things in the wrong order, even to add things in that hierarchy. To avoid human error, we make the machine to do the tasks.
  2. Less time-consuming: Everything takes time from Logging into the console, writing commands to connect to the server. Instead, to avoid being on pins and needles make the machines to do its best. Make it simple and enjoy life.
  3. Reproducibility: Since everyone are keen to do things in the initial stages of activities but after a while, its human tendency to forget things easier and will miss something when trying to reproduce. These inevitable errors can be replaced when the machine does it.

When it comes to automation there are many options, we need to map to the look around and work accordingly. Everything has its own pros and cons, so strive hard to yield the best out of it.

What Is Ansible?

Ansible is a simple and powerful automation language. It helps to automate, accelerate, collaborate and integrate technologies that we already use. Ansible is used to perform the same set of tasks across different servers from the centralized server where Ansible is installed. One advantage of Ansible is completely agentless. It implies no agent needs to be installed on client systems where you want the automation to be done rather provided only SSH communication between client and server need to be established. Here we have some terms for better understanding. All this automation will be easier when there are hundreds of instance in the specified region.

Here are some common terms used when describing Ansible:

Control node: Machine where Ansible is installed and responsible for servers you are managing

Inventory: File that contains information about the servers

Playbook: The Main gateway for automation is defined in terms of tasks through YAML.

Implementing Ansible Solutions with Alibaba Cloud

Prerequisites

Ansible needs to be installed in your host environment.

Installation of Alibaba Cloud Modules

Alibaba cloud modules need to be installed in the control node. (i.e.) where ansible is installed.

We need the Alibaba cloud modules to be installed in ansible before using ansible with Alibaba. There are two ways to install alicloud provider ensure that ansible is already installed in the server.

Modules will get installed in

Configuration file locations:

To ensure check the ansible version with the following command, it points to the currently used configuration file

Let’s make our work much more interesting with a technical guide to get started with the following set of activities.

  1. Starting, stopping, deleting and restarting instance in the specified region
  2. Fetching and gathering facts about the instance in the specified region
  3. Creating disk and attaching the disk to a specific instance.

Ansible playbooks are more efficient when automating the same process for a large number of Virtual machines instead of repeating the tasks individually.

Task 1: Starting, Stopping, Deleting and Restarting Instance in the Specified Region

Starting an ECS instance in Alibaba Cloud with Ansible

The first step is to create the playbook for starting the instance. The yaml code is as follows:

Note: Change the alicloud access key and a secret key of your cloud account

The yaml code is as follows:

The output of the code after running the playbook is as follows:

Before running the code, status of ECS is given below:

Image for post
Image for post

Executing the playbook is as follows:

Image for post
Image for post

After running the playbook, the status of the ECS is as follows:

Image for post
Image for post

Stopping an ECS instance in Alibaba cloud with Ansible

An ECS instance in Singapore region is in running state. The state of the ECS can be stopped using Ansible playbook as follows,

The yaml code for playbook is as follows:

Before executing the code:

Image for post
Image for post

Ansible output is as follows:

Image for post
Image for post

After running the playbook, the status of the instance in ecs console is as follows:

Image for post
Image for post

Restarting an ECS instance in Alibaba cloud with Ansible

The yaml code is as follows (restart playbook):

Compiling the playbook make changes to the ECS instance in the console:

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

The ecs instance which is up and running will again get restarted in the console.

Terminating an ECS Instance in Alibaba Cloud with Ansible

Playbook for terminating instance in ecs console is given below,

While executing this playbook we get the following changes.

Image for post
Image for post

After releasing the instance, it will not be reflected in the console.

Image for post
Image for post

Task 2: Gathering Facts about an ECS Instance in Alibaba Cloud with Ansible

When we are confused to know about the instance details, you can even fetch information about the instance using the below playbook.

From the above playbook, it fetches the instance type as follows,217 types of instances are available.

The second module of the playbook fetch the types of instances available in the specified zone.

135 types of instance with their specifications are listed below:

The third module of the playbook fetch the different types of instances available in the specified family.

Say here ecs.t5 is taken as input, based on the CPU core count 16 varieties of the instance are there in the specified family.

The fourth module of the playbook fetch the different types of instances based on the instance type ids.

Task 3: Creating Disk and Attaching the Disk to a Specific Instance

When additional storage needs to be attached to the instance created, we can create a disk through Ansible.

Creating a disk

The below playbook depicts the way for creating a disk in the specified region.

When executing the playbook for creating disk,

Image for post
Image for post

The disk named testing have been created is reflected in the console.

Image for post
Image for post

Attaching a disk to the instance

This playbook will explain how to attach the disk to the specified instance.

The screenshots depicts the output for executing this playbook and getting reflected in the console.

Image for post
Image for post
Image for post
Image for post

Fetching details about the disk

This playbook describes the details to be gathered about a disk.

Summary

Thus, we have explored about automation with cloud infrastructure focused mainly on Alibaba Cloud Elastic Compute Service (ECS). We have seen about the following starting, stopping, deleting and restarting instance in the specified region, fetching and gathering facts about the instance in the specified region and creating disk and attaching the disk to a specific instance. For further details about automation cloud refer to the upcoming articles.

Reference:https://www.alibabacloud.com/blog/automating-cloud-infrastructure-with-ansible_594283?spm=a2c41.12436879.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store