Understanding Configuration Management with Ansible

Understanding Configuration Management with Ansible

#90 Days of DevOps Challenge - Day 55

What's this Ansible?

Ansible is an open-source automation tool that is used for IT orchestration, configuration management, and application deployment. It simplifies the management of complex infrastructure and allows for the automation of repetitive tasks, making it popular among DevOps teams.

Key features of Ansible include:-

  1. Agentless:-Ansible operates in an agentless manner, meaning it does not require any additional software to be installed on the target systems. It uses SSH or PowerShell to connect to remote systems and execute tasks, making it easy to set up and manage.

  2. Declarative Language:- Ansible uses a simple and human-readable language called YAML (YAML Ain't Markup Language) to define playbooks, which are files that describe the desired state of systems. Playbooks specify the tasks to be executed on remote systems, including configuration changes, software installations, and other operations.

  3. Idempotent Execution:- Ansible ensures idempotent execution, which means that running the same playbook multiple times produces the same results. If a configuration or task has already been applied, Ansible will not make unnecessary changes, improving predictability and reducing the risk of unintended modifications.

  4. Inventory Management:- Ansible allows you to define and manage an inventory of target systems. The inventory can be static or dynamic and can contain information such as host names, IP addresses, and host groups. This enables you to target specific systems or groups of systems for the execution of tasks.

  5. Modular and Extensible:- Ansible provides a wide range of pre-built modules that can be used to perform various tasks, such as managing files, installing packages, or configuring services. Additionally, Ansible is extensible, allowing you to create custom modules to meet specific requirements.

  6. Integration and Community:- Ansible integrates well with other tools and systems, including cloud platforms, orchestration frameworks, and configuration management databases (CMDBs). It has a large and active community that contributes to its development, provides support, and shares modules and playbooks through the Ansible Galaxy.

Ansible is often used for tasks such as provisioning infrastructure, configuring servers and network devices, deploying applications, and managing the lifecycle of systems. It provides a streamlined and efficient way to automate IT operations, enabling organizations to achieve greater scalability, reliability, and productivity in their infrastructure management processes.

Task-01

Installation of Ansible on AWS EC2 (Master Node)

Step 1:- Firstly we need to launch EC2 instance "Ansible Master"

Step 2:- Need to go to the machine and make sure we have updated it

Need to Ansible below command for installing Ansible in the master machine

Ansible Installation

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

Now let's check whether Ansible is installed or not

Task-02

  • read more about Hosts file sudo nano /etc/ansible/hosts ansible-inventory --list -y

The Ansible hosts file is a configuration file that defines the inventory of hosts or groups of hosts that Ansible can manage. It is a text file that follows a simple INI-like format. The default location for the hosts file is /etc/ansible/hosts on the Ansible control node, but you can specify a different location using the -i or --inventory option when running Ansible commands.

sudo nano /etc/ansible/hosts

Once the file is open, you can add the IP addresses or hostnames of the servers you want to manage. The format for adding hosts is as follows:

[servers_name] 
server1 
server2
server3

In this example, servers_name is a user-defined name for the group of hosts, and server1, server2, and server3 are the IP addresses or hostnames of the servers. You can define multiple groups of hosts in the hosts file, each with its own list of hosts.

After you have added the hosts to the file, you can verify the inventory of hosts that Ansible can manage using the ansible-inventory command with the --list and -y options:

ansible-inventory --list -y

This command will display a YAML-formatted list of hosts and their attributes, including the hostnames, IP addresses, and any other defined variables or group memberships.

Task-03

  • Setup 2 more EC2 instances with same Private keys as the previous instance (Node)

  • Copy the private key to master server where Ansible is setup

  • Try a ping command using ansible to the Nodes.

Step 1:- Now we need to launch 2 more EC2 instances which will be Prod and Test

Step 2:- Now we need to copy the pem file which is private key and need to go to the master /home/ubuntu/.ssh

Need to create new file and past it there

need to give the file permission

sudo chmod 600 ansible_key

Step 3:- Now we need to go /etc/ansible/hosts which is by default the location of file. An Ansible hosts file is a configuration file that contains a list of hosts or servers.

[servers]
server1 ansible_host=54.147.179.71
server2 ansible_host=54.196.183.147

[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansible_key

After you have added the hosts to the file, you can verify the inventory of hosts that Ansible can manage using the ansible-inventory command.

ansible-inventory --list -y

Step 4:- Now we need to ping using below command

ansible all -m ping is used to test connectivity to all hosts in the Ansible inventory using the ping module. It sends a ping command to each host and checks if the host is reachable.

ansible all -m ping

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any queries or corrections to be done to this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Printable, customizable thank you card templates | Canva

Did you find this article valuable?

Support Saikat Mukherjee by becoming a sponsor. Any amount is appreciated!