Lecture 04 – ANSIBLE INSTALLATION AND SSH AUTHENTICATION
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!
COPY & RUN –>$ the below commands
sudo apt update
sudo apt install software-properties-common
COPY & RUN –>$ the below commands
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt install ansible
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
