Day 55 Task: Understanding Configuration Management with Ansible
Ansible simplifies and streamlines the management of IT infrastructure and applications
๐ถ What's this Ansible?
Ansible is an open-source automation tool or platform, used for IT tasks such as configuration management, application deployment, intraservice orchestration, and provisioning.
Here are some key features and concepts associated with Ansible:
Agentless: Ansible uses an agentless architecture, meaning it doesn't require any software or agents to be installed on the target systems. Instead, it relies on SSH or other secure communication methods to connect to and manage remote hosts, which makes it easy to get started and maintain.
Playbooks: Ansible automation is defined in Playbooks, which are written in YAML (Yet Another Markup Language). Playbooks describe a set of tasks and configurations to be applied to target hosts. These tasks can include actions like package installation, file copying, service management, and more.
Modules: Ansible uses modules to perform specific tasks on target systems. Modules are included in Playbooks and provide a way to interact with the underlying infrastructure. Ansible comes with a large collection of built-in modules, and you can also create custom modules to suit your needs.
Inventory: The Inventory file is used to define and organize the hosts or groups of hosts that Ansible will manage. It can be a static text file or a dynamic inventory source (e.g., AWS EC2 instances, cloud providers, or other databases).
Idempotent: Ansible enforces the principle of idempotence, which means that running an Ansible playbook multiple times has the same result as running it once. This ensures that the system's configuration remains consistent, and you can safely rerun Ansible Playbooks.
Roles: Roles are a way to organize and package Playbooks, variables, and other Ansible components into reusable and shareable units. Roles help in maintaining a clean and modular codebase.
Ad-hoc Commands: In addition to Playbooks, Ansible allows for ad-hoc commands that can be run from the command line to perform quick, one-off tasks on remote hosts.
Community and Extensibility: Ansible has a large and active community that contributes to the development of Ansible roles, modules, and playbooks. You can easily extend Ansible's functionality with custom scripts and modules.
Integration: Ansible can be integrated with various infrastructure and application components, making it a valuable tool in DevOps and IT automation. It supports integrations with cloud providers, configuration management databases (CMDBs), monitoring tools, and more.
๐ถ Task-01: Installation of Ansible on AWS EC2 (Master Node)
sudo apt-add-repository ppa:ansible/ansible
sudo apt update -y
sudo apt install ansible -y
To install Ansible on an AWS EC2 instance and set it up as a master node, you can follow these steps:
Step 1: Launch an EC2 Instance
Log in to your AWS Management Console.
Navigate to the EC2 dashboard.
Launch a new EC2 instance.
Configure the instance with appropriate security groups and key pairs. Make sure you have SSH access to the instance.
Launch the instance.
Connect to Your EC2 Instance
Step 2: Add Ansible PPA repository by using the below command
sudo apt-add-repository ppa:ansible/ansible
Step 3: Update the System
Update the package manager and upgrade the system packages to the latest versions:
sudo apt update -y
Step 4: Install Ansible
sudo apt install ansible -y
Step 5: Verify Ansible Installation
You can verify the installation by checking the Ansible version:
ansible --version
This should display the installed Ansible version.
๐ถ Task-02: Read more about the Host file
Ansible Inventory - Ansible automates tasks on managed nodes or โhostsโ in your infrastructure, using a list or group of lists known as inventory. You can pass host names at the command line, but most Ansible users create inventory files. Your inventory defines the managed nodes you automate, with groups so you can run automation tasks on multiple hosts at the same time. Once your inventory is defined, you use patterns to select the hosts or groups you want Ansible to run against.
Create an inventory file in the below path and name the file as host.
sudo vim /etc/ansible/hosts
sudo vim /etc/ansible/hosts
Before that, we need to create two Ansible node servers which will be connected to the Ansible master server.
Assign the values in the file as shown in the below screenshot and save.
[servers] server1 ansible_host=<server1_ip> server2 ansible_host=<server2_ip> [all:vars] ansible_user=ubuntu ansible_ssh_private_key_file=~/.ssh/id_rsa ansible_python_interpreter=/usr/bin/python3
Let's verify the inventory that we have created.
ansible-inventory --list -y
ansible-inventory --list -y -i /etc/ansible/hosts
๐ถ Task-03: Setup 2 more EC2 instances with the same Private keys as the previous instance (Node)
Setup 2 more EC2 instances with the same Private keys as the previous instance (Node)
We have created the two servers and set up the server to master the above task.
Copy the private key to the master server where Ansible is set up.
Try a ping command using Ansible to the Nodes.
Now, use the ping command with the input of inventory file to it so that it will ping both the node servers.
ansible all -m ping -u ubuntu
We can see both pings are successful which indicates servers are in active states.
That's it! We have now set up two additional EC2 instances, copied the private key to the Ansible master, and tested Ansible's connectivity to the nodes.
Happy Learning :)
If you find my blog valuable, I invite you to like, share, and join the discussion. Your feedback is immensely cherished as it fuels continuous improvement. Let's embark on this transformative DevOps adventure together! ๐ #devops #90daysofdevop #AWS