Skip to content

Instantly share code, notes, and snippets.

@awadhwana
Last active February 29, 2020 13:07
Show Gist options
  • Save awadhwana/8de6f3e553e81f1be9c546fc6930bc5c to your computer and use it in GitHub Desktop.
Save awadhwana/8de6f3e553e81f1be9c546fc6930bc5c to your computer and use it in GitHub Desktop.
Creating EC2 instance using Ansible
- name: AWS-Docker-Ansible Setup
hosts: localhost
connection: local
gather_facts: False
vars_files:
- vars/variables.yml
tasks:
- name: Create security group
ec2_group:
name: "AWS-Docker-Ansible-group"
description: "A Security group"
aws_access_key: "{{ AWS_ACCESS_KEY }}"
aws_secret_key: "{{ AWS_SECRET_KEY }}"
vpc_id: "{{ vpc_id }}"
region: "{{ region }}"
rules:
- proto: tcp
ports:
- 80
cidr_ip: 0.0.0.0/0
rule_desc: allow all on port 80
- proto: tcp
ports:
- 443
cidr_ip: 0.0.0.0/0
rule_desc: allow all on port 443
- proto: tcp
ports:
- 22
cidr_ip: 0.0.0.0/0
rule_desc: allow all on port 22
register: blog_security_group
- name: Create EC-2 instance
ec2:
aws_access_key: "{{ AWS_ACCESS_KEY }}"
aws_secret_key: "{{ AWS_SECRET_KEY }}"
key_name: "{{ key_pair }}"
instance_type: "{{ instance_type }}"
vpc_subnet_id: "{{ vpc_subnet_id }}"
group_id: "{{ blog_security_group.group_id }}"
image: "{{ ami }}"
wait: true
region: "{{ region }}"
count: 1
volumes:
- device_name: /dev/sda1
volume_type: gp2
volume_size: "{{ volume_size }}"
delete_on_termination: false
instance_tags:
Name: "{{ instance_name }}"
assign_public_ip: yes
#TO RUN
#place the variables.yml file in var folder in the same dir
#ansible-playbook ec2.yml
key_pair: *YOUR KEY PAIR(.pem file)*
instance_type: "t2.micro"
vpc_id: *vpc id*
vpc_subnet_id: *vpc subnet id*
ami: *image key*
region: "ap-south-1"
volume_size: "20"
instance_name: *YOUR INSTANCE NAME*
AWS_ACCESS_KEY: *YOUR ACCESS KEY*
AWS_SECRET_KEY: *YOUR SECRET KEY*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment