-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2_launch.yml
72 lines (64 loc) · 2.33 KB
/
ec2_launch.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
---
- 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: ubuntu # Change the security group name here
image: ami-c4ae7ea7 # Change the AMI, from which you want to launch the server
region: ap-southeast-1 # Change the Region
keypair: ubuntu # Change the keypair name
count: 1
# Task that will be used to Launch/Create an EC2 Instance
tasks:
# - name: Create a security group
# local_action:
# module: ec2_group
# name: "{{ security_group }}"
# description: Security Group for webserver Servers
# region: "{{ region }}"
# rules:
# - proto: tcp
# type: ssh
# 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
# rules_egress:
# - proto: all
# type: all
# cidr_ip: 0.0.0.0/0
- name: Launch the new EC2 Instance
local_action: ec2
group={{ security_group }}
instance_type={{ instance_type}}
image={{ image }}
wait=true
region={{ region }}
keypair={{ keypair }}
count={{count}}
register: ec2
- name: Add the newly created EC2 instance(s) to the local host group (located inside the directory)
local_action: lineinfile
dest="./hosts"
regexp={{ item.public_ip }}
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: Ansible-webserver