Day 55: Understanding Configuration Management with Ansible

Day 55: Understanding Configuration Management with Ansible

🙏 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

👋 Conclusion :

In this blog, we discussed what Ansible is, the purpose of the host file in Ansible, and demonstrated how to connect a master node to a slave node. We will cover advanced topics in a future post.

Thank you for reading!

Contact me on Linkedin

Check out my GitHub for more resources 📚