forked from ansible/ansible-examples
-
Notifications
You must be signed in to change notification settings - Fork 3
/
site.yml
85 lines (74 loc) · 2.14 KB
/
site.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
---
- name: launching an ec2 instance
hosts: localhost
connection: local
gather_facts: false
# vars_files:
# - keypair.yml
tasks:
- name: search for the latest rhel 7 ami
ec2_ami_find:
region: us-west-2
owner: "309956199498"
name: "RHEL-7.2*"
register: find_results
- debug: var=find_results verbosity=5
- name: find a subnet id
ec2_vpc_subnet_facts:
aws_access_key: "{{ec2_access_key}}"
aws_secret_key: "{{ec2_secret_key}}"
region: us-west-2
register: subnet_ids
- debug: var=subnet_ids verbosity=5
- name: create a security group
ec2_group:
name: example
description: "security FTW"
region: us-west-2
# aws_access_key: "{{ec2_access_key}}"
#aws_secret_key: "{{ec2_secret_key}}"
vpc_id: "{{subnet_ids.subnets[0].vpc_id}}"
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 8080
to_port: 8080
cidr_ip: 0.0.0.0/0
register: my_security_group
- name: launch an ec2 instance
ec2:
# aws_access_key: "{{ec2_access_key}}"
# aws_secret_key: "{{ec2_secret_key}}"
instance_type: m3.large
region: us-west-2
image: "{{find_results.results[0].ami_id}}"
group_id: "{{my_security_group.group_id}}"
key_name: gabemuniz
volumes:
- device_name: /dev/sda1
volume_size: 30
volume_type: gp2
instance_tags:
Name: Test-Tower
wait: yes
vpc_subnet_id: "{{subnet_ids.subnets[0].id}}"
assign_public_ip: yes
register: ec2
- debug: var=item.public_ip verbosity=5
- name: add new instance into inventory
add_host: hostname={{item.public_ip}} groupname=tower
with_items: "{{ec2.instances}}"
- name: wait for ssh
wait_for: host="{{item.public_dns_name}}" port=22 delay=60 timeout=320 state=started
with_items: "{{ec2.instances}}"