Day 57: Ansible Hands-on with video

Vinay KumarVinay Kumar
3 min read

Task-01

Write a Blog explanation for the ansible video

Create an EC2 instance

image.png

Connect to your EC2 instance using SSH.

image.png

Add the Ansible PPA repository using the following command:

sudo apt-add-repository ppa:ansible/ansible

image.png

Use the commands below to update the package.

sudo apt update

image.png

Install Ansible using the following command:

sudo apt install ansible

image.png

The following command can be used to determine the Ansible version after installation is complete:

ansible --version

image.png

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

Using the same private keys as the ansible-demo-master instance, launch 2 new EC2 instances.

image.png

To install Ansible, copy the private key to the master server.

Copy the private key from your local.

image.png

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

image.png

You can SSH into two new server instances from the master instance by using your private key.

  1. SSH into an ansible-server1 instance
sudo ssh -i /key-path ubuntu@public-ip-address

image.png

2. SSH into an ansible-server2 instance

image.png

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

Create a new folder named Ansible. Inside the folder, create a host file, which is an inventory file for Ansible. add the IP addresses of the servers inside hosts file.

image.png

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

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

image.png

Try sending a ping command using Ansible to the nodes.

image.png

The failure of the given authentication mechanism, most likely public key authentication, prevents Ansible from establishing an SSH connection to the remote host. also, Verify the remote host's private key file's permissions. Use the chmod command to grant the crucial file permissions.

image.png

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.

image.png

ansible command to check the free memory or memory usage of hosts:

image.png

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

ansible ad hoc command to check uptime

image.png

-a uptime: specifies the arguments to pass to the command module, which in this case is simply the uptime command.

I sincerely appreciate your patience as I begin to publish my first articles on the hashnode. Please contact me if you have any questions.

0
Subscribe to my newsletter

Read articles from Vinay Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Vinay Kumar
Vinay Kumar