Configuration Management And Automation With Ansible

  • By
  • August 22, 2019
  • Network Automation
Configuration Management And Automation With Ansible

Configuration Management And Automation With Ansible

Introduction

I have written two blogs on Network Automation i.e. DevNet-Future for Network Automation and Network Automation using Python Programming that is base on python automation with docker container and networking devices. But this is my first blog based on Ansible tool i.e. “Configuration Management And Automation With Ansible”. I hope you will enjoy reading it.

What Is Ansible?

Ansible is an open-source tool used for IT Configuration Management, Deployment & Orchestration. The main objective of this product is to provide large productivity gains to a wide variety of automation challenges. This tool is very easy and simple to use yet powerful enough to automate complex multi-tier IT application environments.

On this blog you will be learning:

  • Why do we need Ansible?
  • Advantages of using Ansible
  • Ansible Architecture
  • Ansible in DevOps
  • Ansible Vs another Configuration management tools
  • Ansible Setup with Examples

 

Please Refer Below Video For More Information About Ansible and It’s Installation

https://youtu.be/ftpE4xAj0oU

Why Do We Need Ansible?

Well before I tell you what is Ansible, it is important to understand the problems that were faced by an administrator before Ansible.

Previously the process of network deployment and managing servers reliably and efficiently has been a challenge. In the past generation, system administrators managed servers by hand, installing software, changing configurations, and administering services on individual servers.

As data centers grew, and in this data centers most of the hosted applications are more complex, because of that administrators realized they couldn’t scale their manual systems management as fast as the applications they were enabling. It also hampered the speed and performance of the work of the developers since the development team was agile and releasing software frequently, but IT operations were spending more time configuring the systems. That’s why server provisioning and configuration management tools came into the picture.

Previously administrator tasks make things very complicated and time-consuming. But let me tell you that there is a solution to the above-stated problem. The solution is – Ansible.

But before I go ahead to explain to you all about Ansible, let me get you familiarized with few Ansible terminologies:

Common Terms in Ansible

Ansible Server: The machine where Ansible is installed, It is responsible for running the provisioning on the servers you are managing.

Inventory: Inventory is host information file. It contains information about the servers you are managing int form of DNS name or IPs. It is like a hosts file.

Playbook: It is a script file. It contains all configuration commands with host information. This file performs automation through the tasks using YAML format.

Task: It is a small part of the playbook that defines a single procedure to be executed, e.g. Manage services, Install a package.

Module: A module is set off in build programs in Ansible, used to run different tasks. Ansible supports multiple built-in modules, but you can also create your own custom ones.

Role: A predefined way for organizing playbooks with the help of templates and other files in order to facilitate sharing and reusing portions YAML code.

Play: A provisioning of task form start to end is called a play. Like execution of a program is called a process same thing is here execution of a playbook is called a play.

Facts: It acts like a Global variable containing information about the system, like an operating system or network interfaces.

Handlers: We can use handler if you want to trigger service status changes, like starting, stopping and restarting a service. Ansible is a simple and really helpful tool that allows you to create groups of machines, describe how these machines should be configured or what actions should be taken on them.

No other agent tool is installed on the node machines. It uses SSH to connect to the nodes. Ansible only needs to be installed on the control machine (the machine from which you will be running commands) which can even be your laptop.

I am not boasting when I say that Ansible has filled up all the holes in Configuration Management and IT Orchestration world. You will know it too when you take a look at the benefits of Ansible mentioned below:

For Free, Demo classes Call: 7798058777

Registration Link: Click Here!

Advantages Of Using Ansible 

Simple: Ansible uses a simple syntax written in YAML (yet another markup language) called playbooks. YAML is a high-level human-readable language it is used for data serialization. It is extraordinarily simple. So, no special coding skills are required and even people are not from an IT background they are also able to understand the syntax and meaning of the Ansible code. And able to write own playbook for network & system configuration.

Advantages Of Using Ansible

Agentless: Ansible is completely agentless. Another configuration management tools like Chef and Puppet depend on client-side agent tool. There are no agents/software or additional firewall configuration that you need to install on the client systems or hosts which you want to automate. You no need to do separate set up for infrastructure management which includes managing your entire systems, network, and storage. Ansible also reduces the effort required for your technical team to start automating right away and achieving continuous deployment.

Powerful & Flexible: Ansible is a powerful tool because of its features that can enable you to model even the most complex IT workflows. It is more flexible so we can perform a self-sufficient task with automation, we just need to think out-of-the-box and you are ready to use everything that is needed to manage the infrastructure, networks, operating systems, and services that you are already using. Ansible provides you with hundreds of advance and automation supportable modules to manage each and every service and automation task. With the help of Ansible’s capabilities allow you to orchestrate the entire application environment regardless of where it is deployed.

Efficient: Other configuration management tools depend on agents tools but no extra software is required on your servers means more resources for your applications. Ansible modules also work via JSON, JSON programming language is already known to the users so Ansible is extensible with different modules that’s why Ansible is easy to learn and support most of the automation task. Ansible introduces modules as basic building blocks for your software and you can use these modules like a goal in a maven. So, you can even customize these modules as per your needs and you are ready to achieve your goals. For e.g. If you have an existing message sending module which sends messages in plain-text, and you want to send images too, you can add image sending features on top of it.

What is Ansible & its Architecture?

Ansible architecture is very simple and straightforward. You can refer below diagram to understand the Ansible architecture:

What is Ansible & its Architecture

As you can see, in the above diagram, the Ansible automation engine directly interacts with the users who write playbooks to execute the task using Ansible Automation engine. It also interacts with different services like cloud services and the Configuration Management Database (CMDB).

For Free Demo classes Call: 7798058777

Registration Link: Click Here!

The Ansible Automation engine consists of the following terms 

Inventories: It is a file. Ansible inventories are lists of hosts or we can say nodes along with their IP addresses, servers, databases, etc. which needs to be managed by using YAML scripts. Ansible then takes action via a transport – SSH for UNIX, Linux or Networking devices and WinRM for Windows system.

APIs: For cloud service connectivity and different cloud services for public and private cloud as well, Ansible APIs is used as a transporter.

Modules: Modules are executed directly on remote hosts through playbooks. The modules can control system resources, like packages, services or execute system commands or files (anything really). Modules do it by acting on installing packages, making API or system files calls to the service network. Ansible provides more than 450 modules that automate nearly every part of your environment. For e.g.

  1. Cloud Modules like cloud formation which creates or deletes an AWS cloud formation stack;
  2. Database modules like mssql_db which removes MYSQL databases from remote hosts.

Plugins: Plugins allows to execute Ansible tasks as a job build step. Plugins are pieces of code that perform core Ansible’s functionality. Ansible provides numbers of handy plugins, and you can easily write your own plugin as well. For example,

  1. Action plugins are front ends to modules and can execute tasks on the controller before calling the modules themselves.
  2. Cache plugins are used to keep a cache of ‘facts’ to avoid costly fact-gathering operations.
  3. Callback plugins enable you to hook into Ansible events for logging or display purposes.

There are a few more components are also important for understanding Ansible Architecture which is explained below:

Networking: Ansible is used to automate different networks and networking tasks. It uses a data model (a playbook or role) that is separate from the Ansible automation engine that helps up to work on a different project at the same time.

Hosts: The hosts in the Ansible architecture are just node systems which are getting automated by Ansible. It can be any kind of machine – Windows, Linux, RedHat , etc. 

Playbooks: Playbooks are simple files written in YAML format which describes the tasks to be executed by Ansible. Playbooks can declare configurations, but they can also monitor the steps of any manual ordered process, even if it contains jump and logical statements. They can launch tasks synchronously or asynchronously so we can work with playbook as per your project with the different logical schema.

CMDB: It is a central repository that acts as a data warehouse for IT installations and configurations. It manages data related to a collection of different IT assets (commonly referred to as configuration items (CI) like YAML configuration files), and it also describes relationships between such assets.

Cloud: It is a network of remote servers hosted on the Internet to store, manage, and process data, rather than a local server. You can launch your resources and instances on the cloud and connect to your servers.

 Ansible in DevOps

In DevOps, as we know the development team and operations teamwork is an integrated way. For modern test-driven application design this integration is very important. Hence, Ansible engine integrates this by providing a stable environment to both development and operations procedure, so we get a smooth orchestration result. Refer below image to see how Ansible fits into DevOps lifecycle:

 Ansible in DevOps

Now it’s time to discuss how Ansible manages the entire DevOps infrastructure. When developers start to think about infrastructure is a part of their application i.e as Infrastructure as Code (IaC), stability and performance become normative. The process of provisioning and managing computing infrastructure (bare-metal servers, processes, virtual servers, etc.) is called Infrastructure as a Code. We are doing these configurations of networking devices through machine-processable definition files, instead of doing physical hardware configuration or the use of interactive configuration tools. This is a condition where Ansible automation plays a major role.

For Free Demo classes Call: 7798058777

Registration Link: Click Here!

In DevOps, Sysadmins work tightly with developers, development velocity is improved, and more time is spent doing activities like performance tuning, experimenting, and getting things done, and less time is spent fixing problems. Please refer below diagram to understand how the tasks of sysadmins and other users are simplified by Ansible.

DevOps infrastructure

Ansible Vs another Configuration management tools

Ansible Vs another Configuration management tools

Ansible Popularity

As per my survey, Ansible is a most popular tool as compare to another configuration management tool.

Ansible Popularity

Ansible Installation process

Now it’s time to some practical task so let us ready for ansible installation and some basic configuration tasks. I will guide you on how to install Ansible in your Ubuntu 16.04 machine in easy steps. I will also show you how to set up your node machines for Ansible automation and basic example of how to install Apache or httpd server in your node machine from your Ansible control machine.

Install Ansible on Ubuntu 16.04

Step 1: Set EPEL Repository

Ansible package is not available in the default ubuntu machine repositories, so we will enable EPEL repository for Ubuntu 16.04 using the command below:

sudo apt-add-repository ppa:ansible/ansible

Step 2: Update current repositories

sudo apt update

Step 3: Install Ansible

sudo apt install ansible

Step 4: Check Ansible Version

ansible –version

Setup a Client machine with SSH key-based authentication configuration

You’re done with Ansible installation step. Now we will add servers which you want to manage through Ansible. For that, I have created another CentOS VMs which will act as my node machines.

Step 1: 

Enable root ssh access from ansible-client with key-based authentication.

Use below command to check ansible-client machine root authentication is on or not.

Step 2: 

Create RSA key on ansible-server and share with every ansible-client hots

root@ansible-server:~# ssh-keygen

Step 3: 

After the key is generated, the next task is to copy the public key of the Ansible server to its nodes. Use the commands below:

root@ansible-server:~# ssh-copy-id root@ansible-client1

root@ansible-server:~# ssh-copy-id root@ansible-client2

Step 4: 

Now, you can use any editor to write your inventory or specify the IP addresses of your nodes grouped under ‘test-servers’ (or whatever you want your group name to be). I am using the vi editor.

use the following command:

vi /etc/ansible/hosts

For Free Demo classes Call: 7798058777

Registration Link: Click Here!

What is Ansible Ad-hoc commands?                 

– Sometimes we are interested to perform some basic tasks using simple and single commands on the ansible client machines. Using Ansible ad-hoc commands we can do some quick task and perform some simple operations through the command line.

– But real automation works using playbooks only. you may ask me if the playbook is the real automation then Why do we use ad-hoc commands.

– The answer is so simple, Every time you may not need to perform big operations, Some time we are required to perform some simple tasks. So we don’t need to write big playbooks. Also, the ad-hoc command is the easiest way to explore Ansible for beginners.

– For example, you wanted to restart or shut down all of your infrastructure servers or if you wanted to get a hostname of all servers or to get an uptime or os information something like a small and quick task. In that case, you can use ad-hoc commands instead of writing playbooks.

Please Refer Below Video For Ansible Examples

https://youtu.be/HSZ40IC0SLI

Examples using inventory file with Ad-hoc commands

Example 1: Defining client host in an inventory file

root@ansible-server:~# vim /etc/ansible/hosts

[clients]

ansible-client

[webservers]

ansible-client ansible_user=root

root@ansible-server:~# ansible all -m ping

root@ansible-server:~# ansible clients -m ping

root@ansible-server:~# ansible webservers -m ping

where,

-m : module

-a : argument

Example 2: How to take help for Ansible Commands

root@ansible-server:~# ansible-doc -l

root@ansible-server:~# ansible-doc shell

root@ansible-server:~# ansible-doc yum

Example 3: Basic Ansible Ad-hoc Commands

root@ansible-server:~# ansible all -m shell -a “hostname”

root@ansible-server:~# ansible clients -m shell -a “hostname -a”

root@ansible-server:~# ansible clients -m shell -a “pwd”

root@ansible-server:~# ansible webservers -m shell -a “pwd”

Example 4: Create your own inventory file

Create your own inventory file inside any directory and use this ansible host inventory file when you are running Ansible ad-hoc commands.

root@ansible-server:~# echo ansible-client >> ~/host_file

root@ansible-server:~# ansible -i ~/host_file all -m ping

Example 5: Run Ansible Ad-hoc command using predefined ansible modules

root@ansible-server:~# ansible-doc -l

root@ansible-server:~# ansible-doc -l | user

root@ansible-server:~# ansible-doc user

root@ansible-server:~# ansible all -m user -a “name=david password=david”

root@ansible-server:~# ansible all -m shell -a “id david”

Example 6: Install & Remove packages using Ad-hoc command

root@ansible-server:~# ansible all -m shell -a “apt-get  install tree -y”

root@ansible-server:~# ansible all -m shell -a “rpm -qa tree”

root@ansible-server:~# ansible all -m shell -a “apt list –installed | grep -i tree”

root@ansible-server:~# ansible all -i ~/hostfile -m shell -a “apt-get  remove tree -y”

For Free Demo classes Call: 7798058777

Registration Link: Click Here!

Example 7: Install & Remove packages using Ad-hoc command with the help of the module

root@ansible-server:~# ansible-doc yum

root@ansible-server:~# ansible-doc apt

root@ansible-server:~# ansible all -m yum -a “name=httpd state=present”

root@ansible-server:~# ansible all -m yum -a “name=nginx state=present”

root@ansible-server:~# ansible all -m yum -a “name=httpd state=absent”

root@ansible-server:~# ansible all -m yum -a “name=nginx state=absent”

Author:
Onkar Kubade

Call the Trainer and Book your free demo Class for now!!!

call icon

© Copyright 2019 | Sevenmentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*