How to install Ansible on Amazon Linux(EC2)

This tutorial explains, how to Install Ansible on Amazon Linux. Ansible is an automation tool, that is used to manage remote machines. Ansible doesn’t require installation of any agent on any remote Machine for remote management. It manages remote machines over the SSH protocol.

 

Ansible is installed on a Centralized Server and from there all other remote Machines are managed. Centralized Server where Ansible is installed is known as Control node and remote Machines are known as a Managed node.

 

Also Read: How to Install Jenkins on Ubuntu   &   AWS EBS Volume snapshot backup and restore

 

Ansible Installation on Amazon Linux(EC2)

 

Step 1– Update your EC2 Amazon Linux Box

 $ sudo yum update -y

Step 2– Use the yum command to install ansible.

  $ yum install ansible  -y 

Step 3– Check Ansible Version

 $ ansible -version

Basic Configuration of Ansible

Add remote Server’s IP that you want to manage, in the Ansible Inventory file. Ansible Inventory is managed by the file – /etc/ansible/hosts.

Put the IP addresses in the Inventory(hosts) file in the following way.

[WebServer]

10.10.101.39

10.10.101.37

[App Server]

10.10.201.15

10.10.201.21

[DBServer]

10.10.10.11

10.10.10.22

 $sudo nano /etc/ansible/hosts

After adding, check your Inventory list by using the following command.

 $ ansible-inventory -list -y

 

Establish an ssh Connection

Establish an SSH connection between Ansible Server and Remote Hosts

i)Generate a private key and public key in Control node(Ansible Server) using the following command.

  $ ssh-keygen -t rsa

ii) The above command will create two files id_rsa and id_rsa.pub inside the .ssh folder. Copy the content of public-key (id_rsa.pub).

Note: Make sure the permission of id_rsa should be 400.

iii) Create a folder .ssh on the home directory of any user such as ec2-user on Managed node(Remote Servers). Create a file name authorized_keys  inside .ssh folder

$ mkdir /home/ec2-user/.ssh

   $ touch /home/ec2-user/.ssh/authorized_keys

iv) Copy the content of id_rsa.pub from the step ii) inside the file authorized_keys

You can read about Passwordless SSH in more detail in Passwordless SSH using Private and Public Key

Checking Connectivity

Check connectivity to all hosts using the following command.

  $ansible all -m ping -u ec2-user

Where ec2-user is the username that is able to ssh remote machines without a password.

Check connectivity to a host group using the following command.

 $ ansible WebServer -m ping -u ec2-user

I hope you enjoyed this tutorial and learned to install Ansible on Amazon Linux. If you think this is really helpful, please do share this with others as well. Please also share your valuable feedback, comment or any query in the comment box. I will really happy to resolve your all queries any.

Thank You

If you think we helped you or just want to support us, please consider these:-

Connect to us: Facebook | Twitter

You may also like…

2 Responses

  1. Sumit says:

    If we copy the public key from Control Node to the Managed Host, then we won’t be able to SSH to the Managed Host from our local system. Right? How to fix that?

    • cchakravarty says:

      HI Sumit, thanks for your comment !! . You can use same private key to access managed host from your local or create a different key for this. Copy the content of private key(id_rsa) in you local machine home direcory say in a file name magange.pem and use ssh commmand to access it. Make sure the permission ofnewly created file manage.pem should be 400
      sudo ssh -i path/to/manage.pem [email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *