/
webserver.yml
93 lines (86 loc) · 2.85 KB
/
webserver.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
---
- name: WebServer | Create WEB Security Group
local_action:
module: ec2_group
name: "{{ item.sg_name }}"
description: "{{ item.sg_description }}"
region: "{{ vpc_region }}" # Change the AWS region here
vpc_id: "{{ vpc.vpc_id }}" # vpc is the resgister name, you can also set it manually
state: present
rules: "{{ item.sg_rules }}"
with_items: "{{ web_security_groups }}"
register: web_sg
- name: WebServer | Tag the WEB Security Group with a name
local_action:
module: ec2_tag
resource: "{{ item.group_id}}"
region: "{{ vpc_region }}"
state: present
tags:
Name: "{{ vpc_name }}-{{ item.item.sg_name }}"
with_items: "{{ web_sg.results }}"
- name: WebServer | Search for Ubuntu AMI
ec2_ami_search:
region: "{{ vpc_region|default('us-east-1') }}"
distro: "{{ linux_distribution }}"
release: "{{ ubuntu_release }}"
store: "{{ ami_store }}"
virt: "{{ virutalization_type }}"
arch: "{{ cpu_architecture }}"
register: imgae_id
- name: WebServer | Create the WebServer Instance(s)
local_action:
module: ec2
region: "{{ vpc_region }}"
group: "{{ web_security_groups[0].sg_name }}"
keypair: "{{ key_name }}"
instance_type: "{{ web_instance_type }}"
image: "{{ imgae_id.ami }}"
vpc_subnet_id: "{{ item }}"
assign_public_ip: True
wait: True
wait_timeout: 600
user_data: |
#!/bin/sh
sudo apt-get install nginx -y
instance_tags:
Name: "{{ vpc_name }}_WEB_Instance"
Environment: "{{ ENV }}"
Role: "{{ server_role }}"
Application: "{{ application }}"
with_items:
- "{{ public_subnet_1 }}"
- "{{ public_subnet_2 }}"
register: web
- name: WebServer | Set the Instances facts
set_fact:
instance_public_ip_1: "{{ web.results[0].instances[0].public_ip }}"
instance_id_1: "{{ web.results[0].instances[0].id }}"
instance_public_ip_2: "{{ web.results[1].instances[0].public_ip }}"
instance_id_2: "{{ web.results[1].instances[0].id }}"
- name: WebServer | Add the newly created EC2 instance(s) to the local host group (located inside the directory)
local_action:
module: lineinfile
dest: "./hosts"
regexp: "{{ item }}"
insertafter: "[webserver]"
line: "{{ item }}"
with_items:
- "{{ instance_public_ip_1 }}"
- "{{ instance_public_ip_2 }}"
- name: WebServer | Wait for SSH to come up
local_action:
module: wait_for
host: "{{ item }}"
port: 22
state: started
with_items:
- "{{ instance_public_ip_1 }}"
- "{{ instance_public_ip_2 }}"
- name: Create SSH Group to login dynamically to EC2 Instance(s)
add_host:
hostname: "{{ item }}"
groupname: webserver
with_items:
- "{{ instance_public_ip_1 }}"
- "{{ instance_public_ip_2 }}"