forked from lorin/ansiblebook
-
Notifications
You must be signed in to change notification settings - Fork 314
/
ec2-vpc-example.yml
executable file
·101 lines (100 loc) · 2.79 KB
/
ec2-vpc-example.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
---
- name: launch webservers into a specific vpc
hosts: localhost
vars:
instance_type: t2.micro
count: 1
region: us-west-1
tasks:
- name: create a vpc
ec2_vpc:
region: "{{ region }}"
internet_gateway: True
resource_tags: { Name: book, env: production }
cidr_block: 10.0.0.0/16
subnets:
- cidr: 10.0.0.0/24
resource_tags:
env: production
tier: web
- cidr: 10.0.1.0/24
resource_tags:
env: production
tier: db
route_tables:
- subnets:
- 10.0.0.0/24
- 10.0.1.0/24
routes:
- dest: 0.0.0.0/0
gw: igw
register: vpc
- set_fact: vpc_id={{ vpc.vpc_id }}
- name: set ec2 keypair
ec2_key: name=mykey key_material="{{ item }}"
with_file: ~/.ssh/id_rsa.pub
- name: web security group
ec2_group:
name: vpc-web
region: "{{ region }}"
description: allow http and https access
vpc_id: "{{ vpc_id }}"
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
- proto: tcp
from_port: 443
to_port: 443
cidr_ip: 0.0.0.0/0
- name: ssh security group
ec2_group:
name: vpc-ssh
region: "{{ region }}"
description: allow ssh access
vpc_id: "{{ vpc_id }}"
rules:
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: 0.0.0.0/0
- name: outbound security group
ec2_group:
name: vpc-outbound
description: allow outbound connections to the internet
region: "{{ region }}"
vpc_id: "{{ vpc_id }}"
rules_egress:
- proto: all
cidr_ip: 0.0.0.0/0
- name: Get the ubuntu trusty AMI
ec2_ami_search: distro=ubuntu release=trusty virt=hvm region={{ region }}
register: ubuntu_image
- name: start the instances
ec2:
image: "{{ ubuntu_image.ami }}"
region: "{{ region }}"
instance_type: "{{ instance_type }}"
assign_public_ip: True
key_name: mykey
group: [vpc-web, vpc-ssh, vpc-outbound]
instance_tags: { Name: book, type: web, env: production }
exact_count: "{{ count }}"
count_tag: { type: web }
vpc_subnet_id: "{{ vpc.subnets[0].id}}"
wait: yes
register: ec2
- name: add the instance to web and production groups
add_host: hostname={{ item.public_dns_name }} groups=web,production
with_items: ec2.tagged_instances
when: item.public_dns_name is defined
- name: wait for ssh server to be running
wait_for: host={{ item.public_dns_name }} port=22 search_regex=OpenSSH
with_items: ec2.tagged_instances
when: item.public_dns_name is defined
- name: configure webservers
hosts: web:&production
sudo: True
roles:
- web