-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeploy.yml
More file actions
167 lines (150 loc) · 5.94 KB
/
deploy.yml
File metadata and controls
167 lines (150 loc) · 5.94 KB
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
#!/usr/bin/env ansible-playbook
---
- name: Provision an EC2 Instance
hosts: local
connection: local
gather_facts: False
tags: provisioning
# Necessary Variables for creating/provisioning the EC2 Instance
vars:
instance_type: t2.micro
security_group: ansible-webserver # Change the security group name here
image: ami-08bac620dc84221eb # ubuntu server 20.04
keypair: ansibleDemo # the unique name of a key pair
target_group_name: webservers # the unique name of the target group the instances will be added
elb_name: webserver-elb # the unique name for the load balancer
instance_tag: webserver # a tag for the instances to refer to them later
region: eu-west-1
vpc_subnets: ['subnet-08332f53', 'subnet-0a12b242']
vpc_id: vpc-6c3db30a
count: 1
# Task that will be used to Launch/Create an EC2 Instance
tasks:
- name: create a new ec2 key pair, returns generated private key
local_action:
module: ec2_key
name: "{{ keypair }}"
region: "{{ region }}"
register: key
- debug: msg="{{ key }}"
- name: write pem file
local_action:
module: copy
content: |
{{ key.key.private_key }}
dest: "./{{ keypair }}.pem"
mode: 0600
when: key.key.private_key is defined
- name: Create a security group
local_action:
module: ec2_group
name: "{{ security_group }}"
description: Security Group for webserver Servers
region: "{{ region }}"
vpc_id: "{{ 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: 8080
to_port: 8080
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: basic_firewall
- name: Launch the new EC2 Instance
with_items: "{{ vpc_subnets }}"
local_action: ec2
group={{ security_group }}
instance_type={{ instance_type}}
image={{ image }}
wait=true
region={{ region }}
count={{count}}
key_name={{ keypair }}
vpc_subnet_id={{ item }}
monitoring=yes
assign_public_ip=yes
register: ec2
- debug: msg="{{ec2}}"
- name: Expand targets for later use
set_fact:
# Sets a list of mount directories
ec2_instances: "\
{{ ec2_instances | default([]) }} + \
{{ item.instances }}"
with_items: "{{ ec2.results }}"
- debug: msg="{{ec2_instances}}"
- name: write hosts file
local_action:
module: copy
src: "./hosts.template"
dest: "./hosts"
mode: 0600
- name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
local_action: lineinfile
dest="./hosts"
insertafter="[webserver]" line="{{ item.public_ip }}"
with_items: "{{ ec2_instances }}"
- name: Wait for SSH to come up
local_action: wait_for
host="{{ item.public_ip }}"
port=22
state=started
with_items: "{{ ec2_instances }}"
- name: Add tag to Instance(s)
local_action: ec2_tag resource="{{ item.id }}" region="{{ region }}" state=present
with_items: "{{ ec2_instances }}"
args:
tags:
Name: "{{instance_tag}}"
- name: Expand targets for later use
set_fact:
# Sets a list of mount directories
webserver_targets: "\
{{ webserver_targets | default([]) }} + \
[ { 'Id': '{{ item.id }}', 'Port': 8080 } ]"
with_items: "{{ ec2_instances }}"
- debug: msg="{{webserver_targets}}"
- name: Create a target group with a default health check
local_action: elb_target_group
name="{{ target_group_name }}"
protocol=http
port=80
region={{ region }}
vpc_id="{{ vpc_id }}"
state=present
targets={{ webserver_targets }}
wait=False
- name: Create an ELB and attach a listener
local_action:
module: elb_application_lb
name: "{{ elb_name }}"
region: "{{ region }}"
security_groups:
- "{{ security_group }}"
subnets: "{{ vpc_subnets}}"
listeners:
- Protocol: HTTP # Required. The protocol for connections from clients to the load balancer (HTTP or HTTPS) (case-sensitive).
Port: 80 # Required. The port on which the load balancer is listening.
# The security policy that defines which ciphers and protocols are supported. The default is the current predefined security policy.
#SslPolicy: ELBSecurityPolicy-2015-05
#Certificates: # The ARN of the certificate (only one certficate ARN should be provided)
#- CertificateArn: arn:aws:iam::12345678987:server-certificate/test.domain.com
DefaultActions:
- Type: forward # Required.
TargetGroupName: "{{ target_group_name }}" # Required. The name of the target group
state: present
register: lb_output
- debug: msg="Remember this url {{lb_output.dns_name}}"