-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaws-tower-ansible-create-entire-vpc-with-instance.yml
166 lines (152 loc) · 5.7 KB
/
aws-tower-ansible-create-entire-vpc-with-instance.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
################################################################################################
# Creating an Entire VPC Environment in AWS #
# #
# By Robert J. Calva - Red Hat LATAM - 2018 #
# #
# NOTE: just set the aws_region, aws_zone, vpc_name, aws_keypair, cidr_block and #
# volume_type variables as you wish! #
# #
# IMPORTANT: regarding storage_type information, please take a look at this link: #
# http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSVolumeTypes.html #
# #
# Variable examples: #
# #
# aws_region could be: sa-east-1 or us-east-1 #
# aws_zone could be: sa-east-1a, sa-east-1b, us-east-1a, us-east-1b #
# name_zone could be: example.com #
# volume_type could be: standard or gp2 #
# #
# Enjoy! #
################################################################################################
- name: Create an Entire VPC Environment in AWS
hosts: localhost
gather_facts: False
connection: local
tasks:
- name: Create the VPC in AWS {{ aws_region }}
ec2_vpc_net:
state: present
name: "Workshop VPC {{ vpc_name }}"
cidr_block: "{{ cidr_block }}"
region: "{{ aws_region }}"
register: vpc
until: vpc is not failed
retries: 3
- name: Create an AWS Security Group for our Workshop
ec2_group:
name: "aws-workshop-sec-group"
region: "{{ aws_region }}"
description: Workshop Security Group
vpc_id: "{{ vpc.vpc.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
register: aws_workshop_sec_group
until: aws_workshop_sec_group is not failed
retries: 3
- name: Create Subnet for VPC {{ vpc_name }}
ec2_vpc_subnet:
region: "{{ aws_region }}"
vpc_id: "{{ vpc.vpc.id }}"
az: "{{ aws_zone }}"
cidr: "{{ cidr_block }}"
wait_timeout: 600
register: subnet
until: subnet is not failed
retries: 10
- name: VPC Internet Gateway is Present for VPC {{ vpc_name }}
ec2_vpc_igw:
region: "{{ aws_region }}"
vpc_id: "{{ vpc.vpc.id }}"
register: igw
until: igw is not failed
retries: 10
- name: VPC Public Subnet Route Table is Present for VPC {{ vpc_name }}
ec2_vpc_route_table:
region: "{{ aws_region }}"
vpc_id: "{{ vpc.vpc.id }}"
subnets:
- "{{ subnet.subnet.id }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ igw.gateway_id }}"
register: routetable
until: routetable is not failed
retries: 3
- name: set variables for instance creation
set_fact:
ec2_vpc_id: "{{ vpc.vpc.id }}"
ec2_subnet_id: "{{ subnet.subnet.id }}"
- name: Creating an AWS Instance in our VPC
ec2:
key_name: "{{ aws_keypair }}"
group: aws-workshop-sec-group
instance_type: t2.micro
image: ami-85241def
wait: yes
wait_timeout: 500
volumes:
- device_name: /dev/sda1
volume_type: "{{ volume_type }}"
volume_size: 10
delete_on_termination: true
termination_protection: no
instance_tags:
Name: "{{ instance_name }}"
exact_count: 1
count_tag:
Name: "{{ instance_name }}"
region: "{{ aws_region }}"
vpc_subnet_id: "{{ ec2_subnet_id }}"
zone: "{{ aws_zone }}"
assign_public_ip: yes
register: ec2
- name: Add new instance to host group
add_host: hostname={{ item.public_ip }} groupname=launched
with_items: "{{ ec2.instances }}"
- name: Wait for SSH to come up
wait_for: host={{ item.public_dns_name }} port=22 delay=60 timeout=320 state=started
with_items: "{{ ec2.instances }}"
- name: Set variable for instance IP
set_fact:
instance_ip: "{{ item.public_ip }}"
with_items: "{{ ec2.instances }}"
- name: Configuring Instance
hosts: launched
gather_facts: False
remote_user: ec2-user
sudo: yes
tasks:
- name: Install Apache
yum:
name: httpd
state: latest
- name: Start Apache Service
service:
name: httpd
state: started
enabled: yes
- name: Sending E-mail to Users
hosts: localhost
gather_facts: False
connection: local
tasks:
- name: Send email to students with AWS information
sendgrid:
api_key: "{{ sendgrid_api_key }}"
subject: "{{ sendgrid_subject }}"
body: "{{ sendgrid_body }}"
to_addresses: "{{ user_email }}"
html_body: yes
from_address: "{{ sendgrid_from_address }}"
delegate_to: localhost