Unleashing the Power of Ansible: Installing and Configuring on AWS and Performing some Tasks

Unleashing the Power of Ansible: Installing and Configuring on AWS and Performing some Tasks

In this article, we'll go through the steps to install Ansible on an AWS EC2 instance and use it for task automation.

Prerequisites

Before we begin, make sure you have the following:

  • An AWS account

  • An EC2 instance

  • SSH access to the EC2 instance

  • Basic knowledge of Linux command-line

Configuring Ansible on AWS EC2 (Master and Worker Nodes)

Ansible is an open-source configuration management and automation tool that allows you to manage your infrastructure as code. It is commonly used for deploying, configuring, and managing applications on various cloud platforms, including AWS. In this article, we'll go through the steps to configure Ansible on AWS EC2 instances (master and worker nodes) to automate the deployment of applications.

Step 1: Update the System

The first step is to update both the master and worker nodes by running the following command on each EC2 instance:

sudo apt update

Step 2: Install Ansible on the Master Node

To install Ansible on the master node, run the following command:

sudo apt-add-repository ppa:ansible/ansible
#This command will add the Ansible PPA repository to your Ubuntu system. After adding the repository, you can proceed to install Ansible by running the following command:
sudo apt install ansible
#This will update the package list and install Ansible on your system.

This will install Ansible on the master node.

Step 3: To Check Ansible install or not

cat /etc/ansible/hosts

This command will display the contents of the Ansible hosts file located at /etc/ansible/hosts. The hosts file is used to define the hosts and groups that Ansible will manage.

Step 4:

sudo nano /etc/ansible/hosts

This command will open the Ansible hosts file in the Nano text editor with root privileges. You can add hosts and groups to the file manually or use dynamic inventory scripts to generate the inventory from external sources.

Once you have made changes to the inventory file, save the file and exit the editor. You can then use Ansible commands to manage the hosts and groups defined in the inventory.

[servers]
server_1 ansible_host=3.15.11.210
server_2 ansible_host=3.135.188.162
server_3 ansible_host=3.138.181.248

We have created a group called [servers] in your Ansible inventory file (/etc/ansible/hosts) with three hosts: server_1, server_2, and server_3. The ansible_host parameter is used to specify the IP address or hostname of each host.

Before Ping To Those Servers We have to Copy Private key file from local to Server(Master)

Follow this steps :

scp -i "sushrut-key.pem" sushrut-key.pem ubuntu@ec2-3-15-34-142.us-east-2.compute.amazonaws.com:/home/ubuntu/.ssh

In this example, we are using the scp command to securely copy the file we are using the -i option to specify the private key file for authentication. We are copying the sushrut-key.pem file from the local machine to the ubuntu user's home directory on the remote server. The ~ character is a shortcut for the home directory of the current user, which in this case is the ubuntu user.

After copying the .pem file to the remote server, you can use the ansible command with the --private-key option to specify the location of the private key file for SSH authentication

[servers:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_ssh_private_key_file=/home/ubuntu/.ssh/sushrut-key.pem

The first variable ansible_python_interpreter is used to specify the path to the Python interpreter on each host. This can be useful if your hosts use a non-standard Python installation, or if you want to use a specific version of Python.

The second variable ansible_ssh_private_key_file is used to specify the location of the private key file that Ansible should use to authenticate to each host. This is the same .pem file that you copied to the remote servers earlier using the scp command.

By adding these variables to the [servers:vars] section, you are setting them as default variables for all hosts in the [servers] group. This means that you do not need to specify them individually for each host, as Ansible will automatically use these variables for each host in the group.

Dont Forget To Give Permission to your private file Read And Execute.

chmod 600 sushrut-key.pem

Finally to check the connectivity of Servers.

ansible servers -m ping