Skip to content

Instantly share code, notes, and snippets.

@jillr
Created May 6, 2020 21:57
Show Gist options
  • Save jillr/a08cd7a22c7ad2ed8e164bddc7e39adb to your computer and use it in GitHub Desktop.
Save jillr/a08cd7a22c7ad2ed8e164bddc7e39adb to your computer and use it in GitHub Desktop.
---
- name: Provision EC2 Instance
hosts: localhost
connection: local
gather_facts: no
vars:
ec2_info:
rhel8:
owners: 309956199498
instance_type: t2.large
os_type: linux
disk_space: 100
architecture: x86_64
filter: 'RHEL-8????_HVM-????????-x86_64-?-Access2-GP2'
username: ec2-user
ec2_keypair: admiller
ec2_region: us-east-1
ec2_security_group: admiller-devnation-ansible-lamp-secgroup
ec2_vpc: "admillertest-vpc"
ec2_vpc_subnet: "admillertest-subnet"
tasks:
- name: find latest RHEL8 ami
ec2_ami_info:
region: "{{ ec2_region }}"
owners: "{{ ec2_info.rhel8.owners }}"
filters:
name: "{{ ec2_info.rhel8.filter }}"
architecture: "{{ ec2_info.rhel8.architecture }}"
register: rhel8_amis
- debug:
var: rhel8_amis
- name: resolve the subnet id
ec2_vpc_subnet_info:
filters:
"tag:Name": "{{ ec2_vpc_subnet }}"
register: vpc_subnet_info
- debug:
var: vpc_subnet_info.subnets.0
- name: get vpc info
ec2_vpc_net_info:
filters:
"tag:Name": "{{ ec2_vpc }}"
register: vpc_info
- debug:
var: vpc_info
- name: save ami for node
set_fact:
rhel8_ami: >
{{ rhel8_amis.images | selectattr('name', 'defined') | sort(attribute='creation_date') | last }}
- name: Create a security group
ec2_group:
name: "{{ ec2_security_group }}"
description: Security Group for DevNation Ansible LAMP Stack Demo
region: "{{ ec2_region }}"
vpc_id: "{{ vpc_info.vpcs.0.id }}"
rules:
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
rules_egress:
- proto: all
cidr_ip: 0.0.0.0/0
register: create_sg
- debug:
var: create_sg
- name: Create EC2 instances for LAMP Stack
ec2:
assign_public_ip: true
vpc_subnet_id: "{{ vpc_subnet_info.subnets.0.id }}"
key_name: "{{ ec2_keypair }}"
group_id: "{{ create_sg.group_id }}"
instance_type: "{{ ec2_info.rhel8.instance_type }}"
image: "{{ rhel8_ami.image_id }}"
region: "{{ ec2_region }}"
volumes:
- device_name: /dev/sda1
ebs:
volume_type: gp2
volume_size: "{{ ec2_info.rhel8.disk_space }}"
delete_on_termination: true
register: node_output
- debug:
var: node_output
- name: Add the instance to our inventory
lineinfile:
dest: "./inventory.ini"
regexp: "{{ node_output.public_ip }}"
insertafter: "[LAMP]"
line: "{{ node_output.public_ip }}"
- name: Wait for SSH to come up
wait_for:
host: "{{ node_output.public_ip }}"
port: 22
state: present
- name: Add tag to Instance(s)
ec2_tag:
resource: "{{ item.id }}"
region: "{{ ec2_region }}"
state: present
args:
tags:
Name: admillerDevNationRHEL8LAMP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment