Ansible Setup on AWS
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”
- Enter name and description for SG and add following rules for SG keep VPC as default.
- 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
- 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
- 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
- Enter Name [Ansible] & Number of instances [3]
2. Select Amazon Linux as AMI
3. Keep Instance Type as t2.micro and select “Proceed without the key pair” for Key Pair
4. for Network Settings choose “Select existing security group” and select Ansible SG
5. Keep everything else as it is and click on Launch Instances
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
Step-3 SSH into all the instances
1. select any one instance and click on the connect option
2. you will be redirected to Connect to Instance page, keep everything as it is and click on connect.
3. Repeat same steps for other two instances as well
Step-4 Configuring ansible
- Open Ansible-CN terminal tab and run below command and hit enter to skip all user input
ssh-keygen2. run below command and copy content of id_rsa.pub from .ssh directory.
vim /home/ec2-user/.ssh/id_rsa.pub3. 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_keys4. 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 -y6. create an inventory of hosts and add Private IP address of both the managed nodes in it
sudo vim /etc/ansible/hostspaste your Private IP addresses of Ansible-1 and Ansible-2 in host file
7. create a config file which ansible by default refers to
sudo vim /etc/ansible/ansible.cfgadd following content which has reference to inventory file in it and save the file.
[defaults]
inventory = /etc/ansible/hosts8. Try to manually SSH into Managed hosts
ssh ec2-user@172.31.0.154type 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 pingIf you get output as following then your ansible is properly configured





