LAUNCHING EC2 INSTANCE AND CONFIGURING WEBSERVER IN MANAGED NODE BY DYNAMIC INVENTORY USING ANSIBLE ON AWS

Prerequisites for this project:

sudo yum update
sudo yum install python3
sudo dnf install python3-pip
pip3 install ansible — user

Step 1. Launch ec2 instance

- hosts: localhost
gather_facts: no
vars_files:
- awspass.yml
tasks:
- name: Launching ec2 Instance
ec2:
key_name: "awskey"
instance_type: "t2.micro"
image: "ami-0ebc1ac48dfd14136"
wait: "yes"
count: 1
vpc_subnet_id: "subnet-11b3b479"
assign_public_ip: yes
region: "ap-south-1"
state: present
group_id: "sg-010258f611d46ff66"
aws_access_key: "{{ myuser }}"
aws_secret_key: "{{ mypass }}"
register: ec2
- debug:
var: ec2.instances[0].public_ipvim awspass.yml
ansible-playbook playbook_name.yml

Step 2. Updating inventory dynamically

wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.pywget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini
chmod  +x ec2.py chmod  +x ec2.ini
AWS_ACCESS_KEY=XXXX
AWS_SECRET_KEY=XXXX
export AWS_REGION='ap-south-1'
export AWS_ACCESS_KEY=XXXX
export AWS_SECRET_KEY=XXXX

Step 3. Configure the web server through ansible.

ansible-galaxy init ec2-web
- hosts: all
- roles: ec2-web
#task for ec2-web


tasks:
- name: "Configure webserver"
package:
name: "httpd"
state: present
- name: "Copy webpage"
copy:
dest: "{{ dcdir }}"
src: "files/home.html"
- name: Start the service
service:
name: "httpd"
state: started
enabled: yes
ansible-playbook web.yml

Thank You….

--

--

--

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

Recommended from Medium

Crypto Basics: What Is ColdStack (CLS)?

Beyond unit tests: an intro to property and law testing in Scala

Why you should be using the Command design pattern for your next OOP project.

Certified Kubernetes Administrator (CKA) 2020 Exam Changes

How to store secrets with Chamber

What on earth is a Strategy pattern?

Cleaning Up Your GitHub

What I love about the rust programming language

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
Abhishek Sharma

Abhishek Sharma

More from Medium

Getting to know about Node JS

CS373 Spring 2022: Priyanka Barve

Version Controll w/ Git & Github

Freestyle Skiing