Sitemap

Ansible Setup on AWS

4 min readJan 9, 2024

--

Press enter or click to view image in full size

Architecture

  • we will setup one controller node and two managed nodes on AWS EC2
  • Ansible will be installed only on controller node and both managed nodes will have connectivity(SSH) with controller node.

Setup

Step-1 Creating Security Group for our instances

  • Go to Security groups in EC2 dashboard and click “Create security group”
Press enter or click to view image in full size
  • Enter name and description for SG and add following rules for SG keep VPC as default.
Press enter or click to view image in full size
  • Now add Inbound rules
  • you will first need to create SG with only one rule “Ec2 Instance Connect” then you can edit it again and add other two rules in which we have referenced the same SG which we just created for Ping and SSH
Press enter or click to view image in full size
  • We will be using this SG for all of our nodes.

Step-2 Launching EC2 instances for nodes

  • Go to EC2 dashboard and click launch instances
Press enter or click to view image in full size
  • We will be launching three instances using Amazon Linux AMI
  • instance type t2.micro
  • We don’t need keypair as we will use EC2 Instance connect for SSH into our nodes
  • for SG we will use Ansible SG which we created in previous step
  • all other options will be as it is
  • follow below attached configuration for launching steps
  1. Enter Name [Ansible] & Number of instances [3]
Press enter or click to view image in full size

2. Select Amazon Linux as AMI

Press enter or click to view image in full size

3. Keep Instance Type as t2.micro and select “Proceed without the key pair” for Key Pair

Press enter or click to view image in full size

4. for Network Settings choose “Select existing security group” and select Ansible SG

Press enter or click to view image in full size

5. Keep everything else as it is and click on Launch Instances

Press enter or click to view image in full size

6. Now you should be able to see three running instances in EC2 dashboard

Get Nehal Patodiya’s stories in your inbox

Join Medium for free to get updates from this writer.

7. Rename Instances as shown below

Press enter or click to view image in full size

Step-3 SSH into all the instances

1. select any one instance and click on the connect option

Press enter or click to view image in full size

2. you will be redirected to Connect to Instance page, keep everything as it is and click on connect.

Press enter or click to view image in full size

3. Repeat same steps for other two instances as well

Step-4 Configuring ansible

  1. Open Ansible-CN terminal tab and run below command and hit enter to skip all user input
ssh-keygen

2. run below command and copy content of id_rsa.pub from .ssh directory.

vim /home/ec2-user/.ssh/id_rsa.pub

3. Open Ansible-1 terminal and paste content os id_rsa.pud from Ansible-CN into “authorized_keys” file in .ssh dir

vim /home/ec2-user/.ssh/authorized_keys

4. Repeat above step for Ansible-2 node as well

5. Open Ansible-CN terminal and run below command to install ansible.

sudo yum install ansible -y

6. create an inventory of hosts and add Private IP address of both the managed nodes in it

sudo vim /etc/ansible/hosts

paste your Private IP addresses of Ansible-1 and Ansible-2 in host file

Press enter or click to view image in full size

7. create a config file which ansible by default refers to

sudo vim /etc/ansible/ansible.cfg

add following content which has reference to inventory file in it and save the file.

[defaults]
inventory = /etc/ansible/hosts

8. Try to manually SSH into Managed hosts

ssh ec2-user@172.31.0.154

type yes if prompted

type exit to get back to Ansible-CN shell

test SSH for both Managed nodes

9. run following command to test ansible’s connectivity to it’s Managed Nodes

ansible all -m ping

If you get output as following then your ansible is properly configured

Press enter or click to view image in full size

--

--