-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2.yaml
78 lines (65 loc) · 2.1 KB
/
ec2.yaml
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
# AWS playbook
---
- hosts: localhost
connection: local
gather_facts: False
vars:
key_name: my_aws
region: ap-southeast-2
# image: ami-0d56f02d7214dd329 # https://cloud-images.ubuntu.com/locator/ec2/
image: ami-03d56f451ca110e99
id: "servian-app-centos"
sec_group: "{{ id }}-sec"
tasks:
- name: Facts
block:
- name: Get instances facts
ec2_instance_facts:
aws_access_key: "{{ec2_access_key}}"
aws_secret_key: "{{ec2_secret_key}}"
region: "{{ region }}"
register: result
- name: Instances ID
debug:
msg: "ID: {{ item.instance_id }} - State: {{ item.state.name }} - Public DNS: {{ item.public_dns_name }}"
loop: "{{ result.instances }}"
tags: always
- name: Provisioning EC2 instances
block:
- name: Upload public key to AWS
ec2_key:
name: "{{ key_name }}"
key_material: "{{ lookup('file', '~/.ssh/{{ key_name }}.pub') }}"
region: "{{ region }}"
aws_access_key: "{{ec2_access_key}}"
aws_secret_key: "{{ec2_secret_key}}"
- name: Create security group
ec2_group:
name: "{{ sec_group }}"
description: "Sec group for app {{ id }}"
# vpc_id: 12345
region: "{{ region }}"
aws_access_key: "{{ec2_access_key}}"
aws_secret_key: "{{ec2_secret_key}}"
rules:
- proto: tcp
ports:
- 22
- 3000
cidr_ip: 0.0.0.0/0
rule_desc: allow all on ssh port and servian web-app on port 3000
register: result_sec_group
- name: Provision instance(s)
ec2:
aws_access_key: "{{ec2_access_key}}"
aws_secret_key: "{{ec2_secret_key}}"
key_name: "{{ key_name }}"
id: "{{ id }}"
group_id: "{{ result_sec_group.group_id }}"
image: "{{ image }}"
instance_type: t2.micro
region: "{{ region }}"
wait: true
count: 1
register: servian_ec2_info
tags: ['never', 'create_ec2']