# Day 57: Ansible Hands-on with video   (Jan 22, 2024)

## **🙏 Introduction:**

In this blog, Today we will write a blog explanation for the [**Ansible Tutorial for DevOps Engineers**](https://www.youtube.com/watch?v=SGB7EdiP39E)

# **🎯Task: 1**

## **Write a Blog explanation for the** [**ansible video**](https://youtu.be/SGB7EdiP39E)

* Create an EC2 instance
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704101185282/08d1461b-3f31-4433-bbd0-a1a260e930dc.png?auto=compress,format&format=webp align="left")

* Connect to your EC2 instance using SSH
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704101335396/860ebe2a-8a49-4d7f-8f81-f219469881c8.png?auto=compress,format&format=webp align="left")

* Add the Ansible PPA repository
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704101778500/167bd2f4-9210-40c2-8674-6d8a8c64c5d5.png?auto=compress,format&format=webp align="left")

* Update the package
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704101885909/73abacc9-0c92-4695-8781-61e0ecfdf550.png?auto=compress,format&format=webp align="left")

* Install Ansible
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704101953774/8459068b-d860-4469-9a37-6da90c2840c0.png?auto=compress,format&format=webp align="left")

* check the version of Ansible
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704101992075/53c14763-8371-4d04-929e-3bf2a2a5f7be.png?auto=compress,format&format=webp align="left")

* Launch three new EC2 instances with same private key as **Ansible-master-node** EC2 instance.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704102123324/347c9db0-33e0-4c0b-8b9b-dc6fbc787f52.png?auto=compress,format&format=webp align="left")

* Copy the private key to master server where Ansible is setup
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704102470923/474ba133-b819-42ce-b439-73ef284297ee.png?auto=compress,format&format=webp align="left")

* In master node where ansible is setup, create a new file at location /home/ubuntu/.ssh and paste private key to that file.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704102554789/d14782f3-0388-4eef-a46f-2982423612dd.png?auto=compress,format&format=webp align="left")

* SSH into Ansible server instances from master instance by using private key
    

```plaintext
sudo ssh -i /key-path ubuntu@public-ip-address
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704102941793/45d69e92-3025-488c-914d-1545ead7b12f.png?auto=compress,format&format=webp align="left")

* Create an inventory file for Ansible that lists the IP addresses of the Ansible server
    
* Create a new folder named ansible, inside folder create hosts file which is inventory file for ansible. add the IP addresses of the servers inside hosts file.
    

```plaintext
[servers]
server1 ansible_host=34.218.229.216 
server2 ansible_host=54.200.111.227
server3 ansible_host=34.216.221.116

[all:vars]
ansible_python_interpreter=/usr/bin/python3
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704103938615/9e8178ab-1a9d-4ee5-be73-8f6cb5796ce3.png?auto=compress,format&format=webp align="left")

* To verify the inventory of hosts
    

```plaintext
ansible-inventory --list -y -i <inventory-file-path>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704104017422/ad2344a0-93b9-4ef6-b627-75254d3e5947.png?auto=compress,format&format=webp align="left")

* Change the private key permission (ansible\_key)
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704104212479/5ff51b2c-371a-46c4-866b-7bb1f2abe439.png?auto=compress,format&format=webp align="left")

* Specify the private key file to use for authentication using the **\--private-key** option when running the Ansible command
    

```plaintext
ansible -i <inventory_file> all -m ping --private-key=<path_to_private_key>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704104612017/5e820ae9-0c31-4ef7-a073-5eb675992f6d.png?auto=compress,format&format=webp align="left")

* Ansible command to check the free memory
    

```plaintext
ansible -i /home/ubuntu/ansible/hosts all -a "free -m" --private-key=/home/ubuntu/.ssh/ansible_key
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704104894604/70e45907-810e-4fd1-993d-4ba3e72d4519.png?auto=compress,format&format=webp align="left")

* Ansible command to check uptime
    

```plaintext
ansible -i /home/ubuntu/ansible/hosts all -a "uptime" --private-key=/home/ubuntu/.ssh/ansible_key
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704105027052/6b20b07d-9c2f-4115-803f-3ba7b6b40cfc.png?auto=compress,format&format=webp align="left")
