Ansible Setup on AWS EC2 Linux Instance

Please visit my website and subscribe to my youtube channel for more articles

For Ansible to work,python and SSH should be configured on all the servers




On AWS EC2 Linux Free Tier Instance, python and ssh both are already installed

Python Version — 2.7.13

Three servers

Ansible control Server ( Install ansible using epel repository)- On AWS you have to enable this file



How to connect between these servers ?

To ping these servers(webserver and dbserver) from ansible control server , you have to add one inbound rule “All ICAMP traffic” in both the instances)

Ansible Control Server

Install Ansible on Redhat

Install Ansible on AWSLinux

yum repolist ( you should see epel)

yum install ansible

Create an entry for all servers in etc/hosts file as shown below

vim etc/hosts

Create one user “ansadm” on all the servers as shown below

After adding you have to do ssh by login as ansadm user. You will get the below error , because ssh is not setup yet

How to Setup SSH

Generate ssh key on ansible control server (Link)

ssh-keygen on ansible control server by login on ansadm ( ssh is user specific)

This will create .ssh folder (/home/ansadm/.ssh)

Create an authorized_keys on botth the servers and copy the key from ansible control server as shown below

[ansadm@ip-172–31–21–35 ~]$ ssh-copy-id -i ansadm@
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/home/ansadm/.ssh/”
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed

/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on the remote system.
(if you think this is a mistake, you may want to use -f option)

[ansadm@ip-172–31–21–35 ~]$ ssh ansadm@
Last login: Thu Jan 11 13:34:31 2018

__| __|_ )
_| ( / Amazon Linux AMI
[ansadm@ip-172–31–19–214 ~]$ exit

Now all three servers are configured, ansible control server can do ssh on both the servers

Change the ownsership of etc/ansible folder to ansadm

chown -R ansadm:ansadm /etc/ansible

vim etc/ansible/hosts


ansible.cfg file ( This is an inventory file)

Ansible commands ( We can run all commands only on control server and all other servers are managed by it)

To install any package you have to be root. So we are making ansadm of controller as a root user on all machines (except controller)

vi /etc/sudoers


Now run the same command with -s option

Ansible Roles

Roles are the next level of abstraction of ansible playbook. Roles are the list of commands that ansible will execute on target machines in given order

Playbook — decides which role is for which target machine

[ansadm@ip-172–31–21–35 ansible]$ mkdir roles/basic
[ansadm@ip-172–31–21–35 ansible]$ mkdir roles/basic/tasks
[ansadm@ip-172–31–21–35 ansible]$ cd roles/basic/tasks
[ansadm@ip-172–31–21–35 tasks]$ vi main.yml

[ansadm@ip-172–31–21–35 ansible]$ cat /etc/ansible/roles/basic/tasks/main.yml

- name: Install ntp
yum: name=ntp state=present
tags: ntp

[ansadm@ip-172–31–21–35 ansible]$ vi playbook.yml
[ansadm@ip-172–31–21–35 ansible]$ ansible-playbook -K playbook.yml

[ansadm@ip-172–31–21–35 ansible]$ cat playbook.yml
- hosts: all
— role: basic

ansible-playbook <playbook> — list-hosts

To check if HTTPd is installed, the easiest way is to ask rpm:

Devops Automation Enginneer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store