Ansible Logo

Setting up Ansible on AWS EC2 Instance

Requirements

Outline

1. Setting up AWS EC2 Instances

$ ssh -i <path to pem>/awssydneya.pem user@<public DNS address>

2. Setting up Ansible

#Perform a quick update on your instance
$ sudo yum update -y

#Install ansible in your EC2 instance
$ sudo pip install ansible
$ ansible --version

(Optional) Setting up Git

$ git --version

3. Setting up Ansible Project

mkdir playbook
cd ./playbook
playbooks
ansible.cfg
hosts
keypair.pem
web-notls.yml
files
nginx.conf
templates
index.html.j2

ansible.cfg File

[defaults]
inventory = hosts
remote_user = ubuntu
private_key_file = /home/ec2-user/playbooks/keyfile.pem

hosts File

$ vim hosts
[webservers]
testserver ansible_host=ec2-3-25-79-101.ap-southeast-2.compute.amazonaws.com
testserverB ansible_host=ec2-13-210-104-229.ap-southeast-2.compute.amazonaws.com
ansible webservers -m ping

Web-notls.yml File

---
- name: Configure webserver with nginx
hosts: webservers
become: True
tasks:
- name: install nginx
apt: name=nginx update_cache=yes
- name: copy nginx config file
copy: src=files/nginx.conf dest=/etc/nginx/sites-available/default
- name: enable configuration
file: >
dest=/etc/nginx/sites-enabled/default
src=/etc/nginx/sites-available/default
state=link
- name: copy index.html
template: src=templates/index.html.j2 dest=/usr/share/nginx/html/index.html
mode=0644
- name: restart nginx
service: name=nginx state=restarted

Keypair.pem File

vim keypair.pem
chmod 700 keypair.pem

nginx.conf File

server {
listen 8080 default_server;
listen [::]:8080 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
server_name server_name ec2-3-25-79-101.ap-southeast-2.compute.amazonaws.com ec2-13-210-104-229.ap-southeast-2.compute.amazonaws.com;location / {
try_files $uri $uri/ =404;
}
}

index.html.j2 File

<html>
<head>
<title>Welcome to ansible</title>
</head>
<body>
<h1>nginx, configured by Ansible</h1>
<p>If you can see this, Ansible successfully installed nginx.</p>
<p>Running on {{ inventory_hostname }}</p>
</body>
</html>

4. Execution

$ ansible-playbook web-notls.yml

Verification

--

--

--

An entry developer wanting to share my knowledge and bring up others. Also an active person just trying to learn from other people mistakes.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Should I backup my Kafka cluster? And how?

Easy Mobile Navigation with only CSS

CSS Grit Layout

Getting Started With Streamlit Web Based Applications

Configuring Subdomains Apache Server

Introducing GemHUB Games : HEROES 9

My experience attending a Bootcamp in Data Analytics in Austin, Texas

Restart docker from another docker on Windows 10 (Windows container)

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
Thomas Suebwicha

Thomas Suebwicha

An entry developer wanting to share my knowledge and bring up others. Also an active person just trying to learn from other people mistakes.

More from Medium

GitLab — Heroku CI/CD Pipeline in 10 minutes.

Avoiding CI/CD dependency conflicts by using Containers

Updating AWS ECS Taskdefintions and Services via Ansible