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

## **🙏 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
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703785618286/baea76ac-4c71-4642-bf2b-05998a4f9155.png?auto=compress,format&format=webp align="left")

* Add the Ansible PPA repository using the following command
    

```plaintext
sudo apt-add-repository ppa:ansible/ansible
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703860617066/bc7aad20-803c-4211-82b8-185195f10e40.png?auto=compress,format&format=webp align="left")

* Now update the Package manager
    

```plaintext
sudo apt update
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703860958213/4a35bb25-8a59-4392-92e3-94e7ef8d0dc4.png?auto=compress,format&format=webp align="left")

* Install Ansible using the following command
    

```plaintext
sudo apt install ansible
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703861002285/8cbeea97-9b23-44fc-b63f-bf8e3e34b45d.png?auto=compress,format&format=webp align="left")

* To check the version of Ansible using the following command
    

```plaintext
ansible --version
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703861049697/7e51380e-e31e-4d32-af51-6534c818b244.png?auto=compress,format&format=webp align="left")

# **🎯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

```plaintext
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

```plaintext
[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.

```plaintext
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
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703787938314/116fe191-cc4b-4a85-b2cc-cf0091888894.png?auto=compress,format&format=webp align="left")

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
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703861324096/7e3c31a7-5c1e-41e8-9275-b45d0b8fedee.png?auto=compress,format&format=webp align="left")

* From our local machine, transfer the private key to the master node..
    

```plaintext
scp -i "ansible-key.pem" ansible-key.pem ubuntu@ec2-34-216-70-154.us-west-2.compute.amazonaws.com:/home/ubuntu/keys
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703861576766/7f2022c0-437b-4559-ba61-efd6d145db03.png?auto=compress,format&format=webp align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703861657141/9dadf711-2f81-4afa-965f-cceb83c610b5.png?auto=compress,format&format=webp align="left")

1. ### **Try a ping command using ansible to the Nodes**
    

* Configure the host file on master machine
    

```plaintext
sudo vim /etc/ansible/hosts
```

* Add our slave’s Ip address here
    

```plaintext
[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
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703862347730/c35cdd08-76e6-465f-8450-29e7c5b58c9f.png?auto=compress,format&format=webp align="left")

* To verify the inventory of hosts
    

```plaintext
ansible-inventory --list
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703862437559/12b4c323-8b6c-46c9-a712-180417197d55.png?auto=compress,format&format=webp align="left")

* Change the private key permission
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703862738380/db565f74-8ce5-4c9a-b10d-b9a2cc03139d.png?auto=compress,format&format=webp align="left")

* To check the nodes are connected
    

```plaintext
ansible -m ping all
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1703862795436/28d0abf6-be2c-4f52-8f60-2ff93391997f.png?auto=compress,format&format=webp align="left")
