Lecture 04 – ANSIBLE INSTALLATION AND SSH AUTHENTICATION

hands-on_image

ANSIBLE INSTALLATION AND SSH KEY AUTHENTICATION

Pre-Requisites:

Deploy 3 Ubuntu EC2 instances, each with the t2.small configuration. Name them “Ansible”, “Host1” and “Host2”

Ensure python is installed on both machines i.e Master and Hosts, by default AWS Ubuntu images have python installed. 

Skip this step if you are setting up ansible on AWS but on other machines be sure to install python using the following command.

				
					sudo apt-get install python3
				
			

Steps To Install Ansible On Master Machine!

Step 01: To update your server and provide essential tools for managing software repositories and adding Personal Package Archives (PPAs) on Debian-based Linux systems.
 

COPY & RUN –>$ the below commands

				
					sudo apt update
				
			
				
					sudo apt install software-properties-common
				
			
Step 02: To add the Ansible repository to the system’s software sources, allowing you to install latest or update Ansible using the package manager and then install Ansible.
 

COPY & RUN –>$ the below commands

				
					sudo apt-add-repository --yes --update ppa:ansible/ansible
				
			
				
					sudo apt install ansible
				
			
To display information about the installed Ansible version, its configuration, and other relevant details.
 

COPY & RUN –>$ the below command

				
					ansible --version
				
			

Steps To Generate SSH Key Pairs For  Secure Authentication

Step 03: To generates SSH key pairs from Ansible master.

COPY & RUN –>$ the below command

				
					ssh-keygen
				
			

Keep the default values and keep pressing enter 4 times, till you reach the following screen

To print the content of the public SSH key “id_rsa.pub” using administrative privileges.

COPY & RUN –>$ the below command

				
					sudo cat ./.ssh/id_rsa.pub
				
			

ENABLE SSH ACCESS AND CREATE INVENTORY LIST

Steps To Enable Keyless SSH Access Between Ansible Master and Hosts.

Launch hosts side by side. Copy the output of Ansible key pairs generated and paste it in the hosts authorized_keys file.

Step 04: To open the “authorized_keys” file on host servers, which is used to store public keys for SSH authentication

COPY & RUN –>$ the below command

Insert the new entry in the second line of the file. Save and exit

				
					sudo nano ./.ssh/authorized_keys
				
			

Press Cntrl O to save and Cntrl X to exit

Keyless access has now been configured between your Ansible master and hosts.

Let’s verify by running a SSH from your Ansible to your hosts.

COPY & RUN –>$ the below command

				
					ssh ubuntu@host-server-IP
				
			

type exit to come out of host server

Steps To Create List of Hostnames or IP addresses on Ansible Master.

Step 05: To create list of host inventory, open the “hosts” file used by Ansible for managing inventory information. This file typically contains a list of hostnames or IP addresses of remote servers that Ansible will manage.

COPY & RUN –>$ the below command

				
					sudo nano /etc/ansible/hosts
				
			

There will be some sample entries, ignore them and move to the bottom line of the text file. Copy and add the inventory list, ensure to add your hosts server IP addresses as shown below. 

				
					[hosts]
host1 ansible_host=host-server-IP
host2 ansible_host=host-server-IP
				
			

Step 06: To perform a ping module (-m) operation on all hosts specified in the inventory.

This tests connectivity by sending ICMP echo requests to the target hosts. If successful, it will show a “pong” response for each host, indicating that the hosts are reachable and responsive.

COPY & RUN –>$ the below command

				
					ansible -m ping all