Friday, November 5, 2021

HOW TO INSTALL ANSIBLE 2 ON AWS EC2 INSTANCE | BASIC ANSIBLE INVENTORY

 Pre-Requsites: 1. AWS Account should be created

                          2. Create 3 (Master & Two nodes) EC2 Instances EC2 Instance Creation


Step 1: Connect Amazon Linux AMI 2 / Ubuntu/ CentOS / SUSE using putty Connect Linux distro`s using Putty

Step 2: Check Ansible version whether it is already installed or not

             $ ansible --version



             $ -bash: ansible: command not found

Step 3: Ansible is available in Amazon Linux Extra topic "ansible2"

           To Install and use the Ansible in EC2 instance

           $ sudo amazon-linux-extras install ansible2



Step 4: Verify the ansible version 

            $ ansible --version



Step 5: Prepare the sample inventory file in Master node and add the Target server`s details 

            $ vi inventory.txt or vim inventory.txt 

 


Step 6: Connect to Target-1 server using inventory file

           $ ansible <ansible-target-server-name> -m ping -i <inventory_file_path>

           $ ansible ansible-host-1 -m ping -i inventory.txt

           You will get "Are you sure you want to continue connecting (Yes/ No)?" type yes 

            Note : you will get the following error due to invalid permissions

            



Step 7: Add keys to "ssh-agent bash" ssh-agent is a program that stores your passphrase and provides it automatically for you when logging in

    $ ssh-agent bash

Step 8: Copy "your-aws.pem" key to Ansible-Master server to make the communication with other target servers

          Step 8.1 :  If key alredy present in your Ansible-Master system.

                           $ cp your-aws.pem ~/.ssh 

          Step 8.2 : If "your-aws.key" not present in the system follow the below commands

                          $ creat a "your-aws.pem" file in Ansible-Master node, copy the text and paste.

                          $sudo chmod 400 ~/.your-aws.pem file 

Step 9: ssh-add is a command for adding SSH private keys into the SSH authentication agent for implementing single sign-on with SSH  .

            $ ssh-add ~/.ssh/your-aws.pem


Step 10: Connect to Target-1 server using inventory file

           $ ansible <ansible-target-server-name> -m ping -i <inventory_file_path>

           $ ansible ansible-host-1 -m ping -i inventory.txt

           


      Step 10.1 Connect to another server.

     $ ansible ansible-host-2 -m ping -i inventory.txt

    You will get "Are you sure you want to continue connecting (Yes/ No)?" type yes 

   Note: To avoid thsi to propmt every time need to modify the value in "ansible.cfg" file in /etc/ansible/ansible.cfg

Edit ansible.cfg using  $ sudo vi /etc/ansible/ansible.cfg

modify the value or uncomment "host_key_checking=false" then save and quit (:wq!) 

now we can connect target server without this prompt.


Step 11: if we want to ping or connect or deploy all servers together we need to add the service to inventory file

 


   Step 12: Connect all server at once using below command

         $ ansible servers -m ping -i inventory.txt

      


   Successfully installed Ansible and created sample inventory to check the connectivity between Master and target nodes.


No comments:

Post a Comment

How to install and setup Kubernetes cluster using kOps in AWS environment

  Kops: Kops is also known as Kubernetes Operations, it is an open-source project which helps you to create, upgrade, destroy, and maintain ...