-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_aws_vm.yml
176 lines (157 loc) · 4.86 KB
/
create_aws_vm.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
167
168
169
170
171
172
173
174
175
176
---
- name: Provision AWS instances
hosts: localhost
connection: local
gather_facts: False
# load AWS variables from this group vars file
vars_files:
- group_vars/all
tasks:
- name: Verify that you have set a value for user_name before running
assert:
that:
- user_name is defined
fail_msg: "You must define your user_name in extra_vars"
success_msg: "user_name set to {{user_name}}"
- name: create a VPC with dedicated tenancy and a couple of tags
ec2_vpc_net:
name: "{{ ec2_vpc_net_name }}"
cidr_block: "{{ ec2_cidr }}"
region: "{{ ec2_region }}"
tags:
Name: "{{ ec2_vpc_net_name }}"
ansible_group: "{{ ec2_ansible_group }}"
Contact: "{{ my_email_address }}"
DeleteBy: Never
tenancy: default
register: my_vpc
- name: dump vpc info for my_vpc
debug:
var: my_vpc
when: debug_flag is defined
- name: create security group "{{ ec2_security_group_name }}"
ec2_group:
name: "{{ ec2_security_group_name }}"
description: security group with rule descriptions
vpc_id: "{{ my_vpc.vpc.id }}"
region: "{{ ec2_region }}"
tags:
Name: "{{ ec2_security_group_name }}"
ansible_group: "{{ ec2_ansible_group }}"
Contact: "{{ my_email_address }}"
DeleteBy: Never
rules:
- proto: icmp
from_port: 8 # icmp type, -1 = any type
to_port: -1 # icmp subtype, -1 = any subtype
cidr_ip: 0.0.0.0/0
rule_desc: allow ping
- proto: tcp
ports: "{{ open_ports_list }}"
cidr_ip: 0.0.0.0/0
rule_desc: allow all on port 22,80
register: my_security_group
- name: dump security group info for {{ my_security_group }}
debug:
var: my_security_group
when: debug_flag is defined
- name: dump security group info for {{ my_security_group.group_id }}
debug:
var: my_security_group.group_id
when: debug_flag is defined
- name: dump security group info for {{ my_security_group.group_name }}
debug:
var: my_security_group.group_name
when: debug_flag is defined
- name: Create {{ ec2_vpc_subnet_name }} subnet
ec2_vpc_subnet:
state: present
vpc_id: "{{ my_vpc.vpc.id }}"
cidr: "{{ ec2_cidr }}"
map_public: yes
tags:
Name: "{{ ec2_vpc_subnet_name }}"
ansible_group: "{{ ec2_ansible_group }}"
Contact: "{{ my_email_address }}"
DeleteBy: Never
register: my_vpc_subnet
- name: dump {{ ec2_vpc_subnet_name }} info
debug:
var: my_vpc_subnet
when: debug_flag is defined
- name: create {{ ec2_igw_name }}"
ec2_vpc_igw:
vpc_id: "{{ my_vpc.vpc.id }}"
state: present
tags:
Name:: "{{ ec2_igw_name }}"
ansible_group: "{{ ec2_ansible_group }}"
Contact: "{{ my_email_address }}"
DeleteBy: Never
register: my_igw
- name: dump {{ ec2_igw_name }} info
debug:
var: my_igw
when: debug_flag is defined
- name: Set up public subnet route table
ec2_vpc_route_table:
vpc_id: "{{ my_vpc.vpc.id }}"
region: "{{ ec2_region }}"
tags:
Name: "{{ ec2_route_table_name }}"
ansible_group: "{{ ec2_ansible_group }}"
Contact: "{{ my_email_address }}"
DeleteBy: Never
subnets:
- "{{ my_vpc_subnet.subnet.id }}"
routes:
- dest: 0.0.0.0/0
gateway_id: "{{ my_igw.gateway_id }}"
register: my_route_table
- name: dump {{ my_route_table }} info
debug:
var: my_route_table
when: debug_flag is defined
- name: dump critical_vm value
debug:
var: critical_vm
when: debug_flag is defined
- name: Launch {{ec2_instance_count}} AWS instances
ec2:
#access_key: "{{ ec2_access_key }}"
#secret_key: "{{ ec2_secret_key }}"
keypair: "{{ ec2_keypair }}"
group: "{{ my_security_group.group_name }}"
group_id: "{{ my_security_group.group_id }}"
type: "{{ ec2_instance_type }}"
image: "{{ ec2_image }}"
region: "{{ ec2_region }}"
vpc_subnet_id: "{{ my_vpc_subnet.subnet.id }}"
instance_tags:
Name: "{{ tower_user_name }}_test"
ansible_group: "{{ ec2_ansible_group }}"
AlwaysUp: "{{ critical_vm }}"
Contact: "{{ my_email_address }}"
DeleteBy: Never
count: "{{ ec2_instance_count }}"
wait: true
register: ec2_result
- 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_result.instances }}"
async: 250
poll: 0
register: wait_result
- name: wait_for - check on async task
async_status:
jid: "{{ item.ansible_job_id }}"
register: job_result
until: job_result.finished
retries: 300
with_items: "{{wait_result.results}}"
...