Understanding Configuration Management with Ansible
#day67 of #90daysofdevops
Introduction:)
๐ Welcome to Day 67 of our exploration into the world of DevOps and automation! ๐
Today, we're set to explore Ansible, a powerful open-source automation tool that plays a crucial role in configuration management, application deployment, orchestrating intra-service operations, and streamlining provisioning.
What is Ansible?
Ansible is an open-source configuration management
, software provisioning
, and application deployment tool
that makes automating your application
deployments and IT infrastructure operation very simply.
Ansible simplifies cross-platform automation for IT professionals, streamlining tasks from application deployment to infrastructure maintenance, all without the need for agents or complex security infrastructure.
Ansible was developed primarily in Python
, harnessing the power of this versatile language for its automation capabilities. ๐
Key Features of Ansible
Agentless Architecture:
- Ansible operates without agents on managed systems, simplifying setup and maintenance.
Declarative Language:
- Uses a declarative language for expressing desired system states, enhancing readability.
Efficient Application Deployment:
- Streamlines the deployment of applications with code management and configuration adjustments.
Versatile Multi-Cloud Support:
- Supports multiple cloud providers, making it adaptable to diverse infrastructure environments.
Human-Readable Playbooks:
- Automation tasks are defined in YAML-formatted playbooks, making them easily understandable and accessible.
Common Use Cases for Ansible
Configuration Management:
- Enforce and maintain consistent configurations across servers and infrastructure components.
Application Deployment:
- Streamline the deployment process, ensuring applications are deployed efficiently and ready to run.
Orchestration:
- Coordinate complex workflows and tasks, ensuring they execute in the desired order for seamless automation.
Automated Updates and Patching:
- Automate the process of updating and patching software across systems, ensuring security and compliance.
Cloud Provisioning:
- Provision and manage resources on various cloud platforms, facilitating dynamic and scalable infrastructure deployment.
Ansible Architecture
Ansible, a straightforward IT automation engine, simplifies tasks such as cloud provisioning, configuration management, and application deployment. Its architecture revolves around nodes, modules, and various components, designed for multi-tier deployments. Here's a quick overview:
Modules
Ansible connects to nodes and sends scripts known as "Ansible modules" to them.
Modules execute tasks on nodes to achieve the desired system state.
No servers, daemons, or databases are required; modules can reside on any machine.
Module Utilities
Functions shared by multiple modules are stored as module utilities to minimize duplication.
For example, common code like URL parsing is stored to enhance efficiency.
Users can create their module utilities, usually in Python or PowerShell.
Plugins
Plugins extend Ansible's core functionality and execute on the control node.
They offer options and extensions for core features such as transforming data and connecting to inventory.
Ansible ships with useful plugins, and users can create custom ones, typically in Python.
Inventory
Ansible represents managed machines in an inventory file, usually in INI or YAML format.
No SSL signing server is needed to add new machines, simplifying the process.
Inventory details can be sourced from various providers like EC2, Rackspace, etc.
Playbooks
YAML-formatted files, known as playbooks, define automation jobs and the desired system states.
Playbooks are executed on the control node, orchestrating multiple tasks across the infrastructure.
They follow a simple and human-readable syntax.
Ansible Search Path
Modules, utilities, plugins, playbooks, and roles can exist in multiple locations.
The search path determines which files Ansible discovers and uses during a playbook run.
It grows incrementally as Ansible finds each playbook or role, appending related directories to the path.
How Ansible works
In Ansible, there are two categories of computers: the control node and managed nodes
. The control node is a computer that runs Ansible. There must be at least one control node, although a backup control node may also exist. A managed node is any device being managed by the control node.
Ansible works by connecting to nodes (clients, servers, or whatever you're configuring) on a network, and then sending a small program called an Ansible module to that node. Ansible executes these modules over SSH and removes them when finished. The only requirement for this interaction is that your Ansible control node has login access to the managed nodes. SSH keys are the most common way to provide access, but other forms of authentication are also supported.
Control Node & Managed Node
Control Node:
The control node is the machine where Ansible is installed and from which automation scripts (playbooks) are executed. It serves as the central point for managing and orchestrating tasks across a set of remote machines (managed nodes). The control node is responsible for storing inventory information, playbooks, and managing the communication and execution of tasks on the managed nodes.
Key components on the control node include:
Ansible Installation:
- The control node requires Ansible installed to manage and automate configurations on managed nodes.
Inventory File:
- The control node's inventory file specifies managed nodes, providing IP addresses, hostnames, and connection details for Ansible interactions.
Playbooks:
- Playbooks, written in YAML, reside on the control node and define tasks executed on managed nodes.
Modules:
- Ansible modules, units of work executed on managed nodes, perform tasks like installing software, copying files, and managing services. They are present on the control node.
Managed Node:
Managed nodes are the machines that Ansible manages and automates. These are the remote servers or devices where configurations are applied and tasks are executed. Managed nodes must have SSH connectivity from the control node, and Python installed (to run Ansible modules).
Key points about managed nodes:
SSH Connectivity:
- Ansible relies on SSH, requiring passwordless authentication. Control node must SSH into managed nodes with specified user.
Python Interpreter:
- Managed nodes need Python (v2.7+) for executing Ansible modules written in Python.
Execution of Tasks:
- Managed nodes execute tasks from playbooks or ad-hoc commands sent by the control node. Ansible orchestrates task execution.
Tasks:)
Task-01
Installation of Ansible on AWS EC2 (Master Node)
For the installation of Ansible on an AWS EC2 instance (Master Node), follow these general steps:
Connect to EC2 Instance:
SSH into your AWS EC2 instance where you want to install Ansible.
Add the Ansible repository:
You need to add the Ansible repository to your system. This repository holds all the important files and stuff that Ansible needs.
sudo apt-add-repository ppa:ansible/ansible
Update Package Lists:
Run the following command to update the package lists:
sudo apt update
Install Ansible:
Install Ansible using the package manager:
sudo apt install ansible
Verify Installation:
Check Ansible version to ensure successful installation:
ansible --version # This command should display the installed Ansible version.
Task-02
Read more about Hosts file
The Ansible hosts file, often named hosts
by convention, serves as the inventory file for Ansible. Its primary role
is to provide Ansible with information about the managed nodes
or hosts that it will interact with during automation tasks. The hosts file plays a crucial role in the configuration management and orchestration process.
โญ Default Location:
- The host file is located at
/etc/ansible/hosts
by default, but you can specify a different path using the-i
option when executing Ansible commands or playbooks.
โญ Viewing and Editing:
You can view and edit the Ansible hosts file using a text editor. For example, using Vim:
sudo vim /etc/ansible/hosts
โญ File Structure:
The hosts file typically follows an INI-like structure, where hosts are grouped under section headers denoted by square brackets. For instance:
[group_name] hostname1 hostname2 hostname3 [another_group_name] hostname4 hostname5
โญ Viewing Host Inventory:
To view the current host inventory configured in Ansible, you can use the
ansible-inventory
command:ansible-inventory --list -y
This command outputs a YAML-formatted list of hosts, displaying their attributes, such as hostnames, IP addresses, and any defined variables or group memberships.
Task-03:
Setting Up Additional EC2 Instances and Ansible Ping
Set Up 2 More EC2 Instances
Create EC2 Instances:
Create two additional EC2 instances on AWS with the same private keys as the previous instance.
Configure Ansible Control Node
Ensure Proper Permissions:
Ensure that the permissions of the private key file are secure. You can set the permissions using the following command:
chmod 400 /path/to/private-key.pem
Transfer Private Key:
Transfer the copied private key to the Ansible control node (master server) using a secure method (e.g., SCP or SFTP).
Make the folder for storing the key
mkdir key
configure the Host file
Open the Ansible hosts file using a text editor. The default path is
/etc/ansible/hosts
Add your EC2 instances to the hosts file. The format is generally
[servers] server1 ansible_host=your_first_server_IP server2 ansible_host=Your_second_server_IP [all:vars] ansible_python_interpreter=/usr/bin/python3 ansible_user=ubuntu ansible_ssh_private_key_file=full_location_of_key
Note: Python is needed in the managed node
That's why here we install the python of version 3
Run Ansible Ping
- Run the command to ping all the servers
ansible servers -m ping
Conclusion:)
Ansible proves to be an exceptional tool for automation, configuration management, and orchestration. Today's tasks, including the installation of Ansible on AWS EC2 instances, configuring the hosts file, and performing basic commands, have showcased the simplicity and power of Ansible.
Connect with me:)
Thank you for diving into this blog with me! I trust you found the information both helpful and enlightening. To stay updated on the latest in DevOps ๐, make sure to follow me. Remember, staying informed means staying ahead in the dynamic world of DevOps!
Feel free to connect with me on:
For more updates and engaging discussions on DevOps, let's connect! ๐ #DevOpsCommunity