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….

--

--

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