Skip to main content

Command Palette

Search for a command to run...

Day 55: Understanding Configuration Management with Ansible (Jan 20, 2024)

Published
3 min read
Day 55: Understanding Configuration Management with Ansible  (Jan 20, 2024)
M

Hey there! I'm currently working as an Associate DevOps Engineer, and I'm diving into popular DevOps tools like Azure Devops,Linux, Docker, Kubernetes,Terraform and Ansible. I'm also on the learning track with AWS certifications to amp up my cloud game. If you're into tech collaborations and exploring new horizons, let's connect!

🙏 Introduction:

In this blog, we'll explore Ansible, an open-source automation tool that's important for managing configurations, deploying apps, organizing intra-service tasks, and helping with provisioning.

🔶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.

🎯Task: 1

  1. Installation of Ansible on AWS EC2 (Master Node)

  • Create an EC2 instance

  • Add the Ansible PPA repository using the following command
sudo apt-add-repository ppa:ansible/ansible

  • Now update the Package manager
sudo apt update

  • Install Ansible using the following command
sudo apt install ansible

  • To check the version of Ansible using the following command
ansible --version

🎯Task: 2

  1. Read more about Hosts file sudo nano /etc/ansible/hosts ansible-inventory --list -y

Ansible hosts file is a configuration file that contains a list of hosts or servers that Ansible can manage. The hosts file is located at /etc/ansible/hosts on the Ansible control node, and it is used to define the inventory of hosts that Ansible can manage.

To edit the hosts file

sudo vim /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

[web_servers]
web1 ansible_host=192.168.1.101
web2 ansible_host=192.168.1.102

[database_servers]
db1 ansible_host=192.168.1.201
db2 ansible_host=192.168.1.202

In this example, we have two groups: web_servers and database_servers, each containing two hosts. You can define various attributes for each host, such as ansible_host (IP address or hostname) and others.

After adding 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

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

🎯Task: 3

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

  • Launch 2 new EC2 instances with same private keys as Ansible-master-node instance

  1. Copy the private key to master server where Ansible is setup

  • Create a directory on the master node named as keys and get the path of the keys directory

  • From our local machine, transfer the private key to the master node..
scp -i "ansible-key.pem" ansible-key.pem ubuntu@ec2-34-216-70-154.us-west-2.compute.amazonaws.com:/home/ubuntu/keys

  1. Try a ping command using ansible to the Nodes

  • Configure the host file on master machine
sudo vim /etc/ansible/hosts
  • Add our slave’s Ip address here
[servers]
host_1 ansible_host=54.184.85.99 
host_2 ansible_host=54.200.206.23

[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/keys/ansible-key.pem

  • To verify the inventory of hosts
ansible-inventory --list

  • Change the private key permission

  • To check the nodes are connected
ansible -m ping all

More from this blog

DevOps Journey with M Hassan

174 posts

I am writing these blogs because I recently completed a comprehensive DevOps course where I gained in-depth knowledge of the topics mentioned. As I progressed through the course, I realized the importance of having a concise and accessible resource to revise and reinforce my understanding of each topic. Therefore, I decided to create cheat sheets in the form of blog posts. These cheat sheets will not only serve as a handy reference for myself but also benefit others who are also interested in mastering DevOps concepts. By documenting each topic and providing concise explanations, I aim to create a valuable resource that simplifies complex concepts and facilitates hands-on practice. This way, I can solidify my own understanding while helping others on their DevOps journey.