-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_statsd_ec2.yml
105 lines (94 loc) · 3.22 KB
/
create_statsd_ec2.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
---
- name: Create an statsd server on EC2
hosts: localhost
connection: local
gather_facts: False
vars:
statsd_instance_name: mao-statsd
statsd_security_group: mao_statsd_sg
statsd_security_group_desc: security group for statsd server
statsd_instance_type: c4.xlarge
statsd_ami_id: ami-29ebb519 # Ubuntu 14.04 TLS HVM
statsd_ebs_device: /dev/xvdb
statsd_volumes:
- device_name: "{{ statsd_ebs_device }}"
device_type: gp2
volume_size: 100
delete_on_termination: false
statsd_user_data: |
#!/bin/bash -ex
exec > >(tee /var/log/user-data.log|logger -t user-data -s 2>/dev/console) 2>&1
# format and mount ebs volume
DEVICE_NAME={{ statsd_ebs_device }}
apt-get install -y -q xfsprogs
mkfs.xfs $DEVICE_NAME
mkdir /ebs_vol
mount $DEVICE_NAME /ebs_vol
cp /etc/fstab /etc/fstab.orig
echo "$DEVICE_NAME /ebs_vol xfs defaults 0 0" >> /etc/fstab
# trigger Tower job callback
TOWER_ADDRESS={{ tower_address }}
HOST_CONFIG_KEY={{ host_config_key }}
TEMPLATE_ID={{ template_id }}
retry_attempts=10
attempt=0
while [[ $attempt -lt $retry_attempts ]]
do
status_code=`curl -k -s -i --data "host_config_key=$HOST_CONFIG_KEY" https://$TOWER_ADDRESS/api/v1/job_templates/$TEMPLATE_ID/callback/ | head -n 1 | awk '{print $2}'`
if [[ $status_code == 202 ]]
then
exit 0
fi
attempt=$(( attempt + 1 ))
echo "${status_code} received... retrying in 1 minute. (Attempt ${attempt})"
sleep 60
done
exit 1
statsd_rules:
- proto: tcp
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
- proto: tcp
from_port: 2003
to_port: 2003
cidr_ip: 0.0.0.0/0
- proto: udp
from_port: 8125
to_port: 8125
cidr_ip: 0.0.0.0/0
statsd_rules_egress:
- proto: all
from_port: all
to_port: all
cidr_ip: 0.0.0.0/0
tasks:
- name: Create securtity group for statsd host
ec2_group:
name: "{{ statsd_security_group }}"
description: "{{statsd_security_group_desc }}"
region: "{{ region }}"
rules: "{{ statsd_rules }}"
rules_egress: "{{ statsd_rules_egress }}"
state: 'present'
- name: Create statsd host
ec2:
region: "{{ region }}"
key_name: "{{ key_name }}"
instance_type: "{{ statsd_instance_type }}"
image: "{{ statsd_ami_id }}"
wait: yes
group: "{{ statsd_security_group }}"
instance_tags:
Name: "{{ statsd_instance_name }}"
vpc_subnet_id: "{{ vpc_subnet_id }}"
assign_public_ip: yes
volumes: "{{ statsd_volumes }}"
user_data: "{{ statsd_user_data }}"
exact_count: 1
count_tag:
Name: "{{ statsd_instance_name }}"