Day 57: Ansible Hands-on with video

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:

  1. Create Ec2 Instance

  2. Launch the instance.

  3. Connect to Your EC2 Instance

Add Ansible PPA repository by using the below command

sudo apt-add-repository ppa:ansible/ansible

sudo apt update -y

Install Ansible

sudo apt install ansible -y

Verify Ansible Installation

You can verify the installation by checking the Ansible version:

ansible --version

Launch 2 new EC2 instances with same private key as ansible-master EC2 instance.

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

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

You can SSH into 2 new server instances from master instance by using private key.

  1. SSH into ansible-server1 instance

  2. SSH into ansible-server2 instance

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

Create an inventory file for Ansible that lists the IP addresses of the two new EC2 instances.

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

you can verify the inventory of hosts using the ansible-inventory command.

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

Try a ping command using ansible to the Nodes.

This error message indicates that Ansible was unable to connect to the remote host using SSH because the authentication method specified (likely public key authentication) failed. also Check the permissions on the remote host's private key file

Give permissions to the key file using chmod command.

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

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

<inventory_file> is the path to the inventory file.

<hosts> is the name or pattern of the host(s) that you want to ping.

<path_to_private_key> is the path to the private key file to use for authentication.

-a "free -m": the -a option specifies the arguments to pass to the command to be executed on the remote hosts. In this case, the free-m command is executed to show the memory usage on each host.

Happy Learning!