Last active
February 13, 2018 13:15
-
-
Save ckolos/94c66056bf22dc5d2421a09cc8953467 to your computer and use it in GitHub Desktop.
VPC Subnet Playbook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: localhost | |
connection: local | |
gather_facts: false | |
vars_files: | |
- subnet_vars.yml | |
vars: | |
azs: "{{ azmap.keys() | sort}}" | |
debug: false | |
tasks: | |
- name: validate vars | |
assert: | |
that: | |
- azmap != '' | |
- azs != '' | |
- region != '' | |
- vgw_cidr != '' | |
- vgw_peer != '' | |
- vpc_cidr != '' | |
- vpc_id != '' | |
- vpc_peer != '' | |
- name: Echo vars | |
debug: | |
msg: "{{item}}" | |
when: false | |
with_items: | |
- | | |
Region : "{{ region }}" | |
VGW CIDR : "{{ vgw_cidr }}" | |
VGW Peer : "{{ vgw_peer }}" | |
VPC CIDR : "{{ vpc_cidr }}" | |
VPC Id : "{{ vpc_id}}" | |
VPC Peer : "{{ vpc_peer }}" | |
Azs : "{{ azs }}" | |
AZMap : "{{ azmap }}" | |
- name: Create public subnets | |
ec2_vpc_subnet: | |
state: present | |
az: "{{item}}" | |
vpc_id: "{{vpc_id}}" | |
region: "{{region}}" | |
cidr: "{{azmap[item].values()[0]}}" | |
map_public: true | |
resource_tags: | |
Name: "pub-{{item}}" | |
register: pubsubnets | |
with_items: "{{azs}}" | |
- name: Create private subnets | |
ec2_vpc_subnet: | |
state: present | |
az: "{{item}}" | |
vpc_id: "{{vpc_id}}" | |
region: "{{region}}" | |
cidr: "{{azmap[item].values()[1]}}" | |
resource_tags: | |
Name: "priv-{{item}}" | |
register: privsubnets | |
with_items: "{{azs}}" | |
- name: Create NAT gateways in public subnets | |
ec2_vpc_nat_gateway: | |
state: present | |
subnet_id: "{{ item.subnet.id }}" | |
region: "{{ region }}" | |
wait: "yes" | |
if_exist_do_not_create: true | |
register: natgws | |
with_items: "{{pubsubnets.results}}" | |
- set_fact: | |
az_subnet_map: "{{ az_subnet_map | default({})|combine({item.subnet.availability_zone: item.subnet.id}) }}" | |
with_items: "{{privsubnets.results}}" | |
- debug: var=az_subnet_map | |
- set_fact: | |
az_nat_map: "{{ az_nat_map | default({})|combine({item.item.item: item.nat_gateway_id}) }}" | |
with_items: "{{ natgws.results }}" | |
- debug: var=az_nat_map | |
- include: route_table.yml | |
vars: | |
priv_subnet_id: "{{ az_subnet_map[route_item] }}" | |
gateway_id: "{{ az_nat_map[route_item] }}" | |
az: "{{ route_item }}" | |
with_items: "{{ azs }}" | |
loop_control: | |
loop_var: route_item |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- name: Echo vars | |
debug: | |
msg: "{{item}}" | |
when: false | |
with_items: | |
- | | |
Az : "{{ az }}" | |
Gateway : "{{ gateway_id }}" | |
Region : "{{ region }}" | |
VGW CIDR : "{{ vgw_cidr }}" | |
VGW Peer : "{{ vgw_peer }}" | |
VPC CIDR : "{{ vpc_cidr }}" | |
VPC Id : "{{ vpc_id}}" | |
VPC Peer : "{{ vpc_peer }}" | |
- name: Create private subnet route table | |
ec2_vpc_route_table: | |
vpc_id: "{{ vpc_id }}" | |
region: "{{ region }}" | |
tags: | |
Name: "{{ az }}_private_subnet_route" | |
routes: | |
- dest: "{{ vpc_cidr }}" | |
vpc_peering_connection_id: "{{ vpc_peer }}" | |
- dest: "{{ vgw_cidr }}" | |
gateway_id: "{{ vgw_peer }}" | |
- dest: 0.0.0.0/0 | |
gateway_id: "{{gateway_id}}" | |
subnets: | |
- "{{priv_subnet_id}}" | |
register: route_table | |
- debug: var=route_table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
azmap: | |
us-east-1a: | |
public_subnet: 10.1.0.0/23 | |
private_subnet: 10.1.10.0/24 | |
us-east-1b: | |
public_subnet: 10.1.2.0/23 | |
private_subnet: 10.1.11.0/24 | |
us-east-1c: | |
public_subnet: 10.1.4.0/23 | |
private_subnet: 10.1.12.0/24 | |
us-east-1d: | |
public_subnet: 10.1.6.0/23 | |
private_subnet: 10.1.13.0/24 | |
us-east-1e: | |
public_subnet: 10.1.8.0/23 | |
private_subnet: 10.1.14.0/24 | |
region: us-east-1 | |
vpc_cidr: 172.31.0.0/16 | |
vpc_id: vpc-deafbeef | |
vpc_peer: pcx-deadbeef | |
vgw_cidr: 10.0.0.0/16 | |
vgw_peer: vgw-beefdeaf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment