-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathplaybook.yml
109 lines (98 loc) · 2.91 KB
/
playbook.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
---
- hosts: localhost
tasks:
- name: Get my current IP address
uri:
url: http://checkip.amazonaws.com/
return_content: yes
register: my_ip
- name: Create simple security group
ec2_group:
name: webservers
description: A security group for my current IP
region: us-west-1
rules:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: "{{ my_ip.content | replace('\n', '') }}/32"
- proto: tcp
from_port: 22
to_port: 22
cidr_ip: "{{ my_ip.content | replace('\n', '') }}/32"
rules_egress:
- proto: tcp
from_port: 80
to_port: 80
cidr_ip: 0.0.0.0/0
register: webservers_group
- name: Create the ELB only listening over port 80
ec2_elb_lb:
name: "{{ application }}-load-balancer"
state: present
region: us-west-1
zones:
- us-west-1a
- us-west-1b
listeners:
- protocol: http
load_balancer_port: 80
instance_port: 80
register: elb
- name: Create EC2 instances
ec2:
key_name: nickhammond
instance_type: t2.nano
image: ami-f2b39792
region: us-west-1
group_id:
- "{{ webservers_group.group_id }}"
- "{{ elb.elb.security_group_ids | first }}"
wait: yes
instance_tags:
application: "{{ application }}"
exact_count: "{{ instance_count }}"
count_tag:
application: "{{ application }}"
register: ec2_instances
- name: Store EC2 instance IPs to provision against
add_host:
hostname: "{{ item.public_ip }}"
groupname: ec2_instance_ips
with_items: "{{ ec2_instances.tagged_instances }}"
- name: Wait for servers to come online
wait_for:
host: "{{ item.public_ip }}"
port: 22
timeout: 180
with_items: "{{ ec2_instances.tagged_instances }}"
- name: Add EC2 instances as known hosts
known_hosts:
name: "{{ item.public_ip }}"
key: "{{ lookup('pipe', 'ssh-keyscan -t rsa ' + item.public_ip) }}"
with_items: "{{ ec2_instances.tagged_instances }}"
- hosts: ec2_instance_ips
remote_user: ubuntu
become: True
tasks:
- name: Install nginx
apt:
name: nginx
state: present
- name: Create default index page
copy:
dest: /usr/share/nginx/html/index.html
content: "Howdy from {{ ansible_hostname }}"
- name: Restart nginx
service:
name: nginx
state: restarted
- hosts: localhost
tasks:
- name: Add each EC2 instance to the ELB
ec2_elb:
state: present
ec2_elbs: "{{ application }}-load-balancer"
region: "{{ item.region }}"
instance_id: "{{ item.id }}"
with_items: "{{ ec2_instances.tagged_instances }}"