-
Notifications
You must be signed in to change notification settings - Fork 3
/
deploy-autoscaling-alb.yml
111 lines (94 loc) · 4.14 KB
/
deploy-autoscaling-alb.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
110
111
---
# ansible-playbook deploy-autoscaling-alb.yml -e project=apollo -e role=app -e env=production
#
# Swap ALB target groups for the specified auto scaling groups by detaching from one group
# and reattaching to another. First, the group specified by `new_group` has all
# its ALB target groups detached. Then, target groups attached to the group specified by
# `current_group` are also attached to the group specified by `new_group`.
# Once the instances in `new_group` are reporting healthy on the newly attached
# target groups, the same target groups are then detched from `current_group`.
#
# The "post-deploy" target group is then attached to the group specified by
# `current_group_name`. This is done to enable the `current_group` instances to
# continue to be accessible in browser, and also to ensure that ALB health
# checks continue to pass.
#
# If there is no live stage group for the environment, then simply transition
# the pre group to live.
- name: Identify auto scaling groups
hosts: localhost
connection: local
pre_tasks:
- name: Validate arguments
assert:
that:
- env is defined
- project is defined
- role is defined
msg: "Missing required arguments: env and/or project and/or role"
tasks:
- name: Identify the pre stage group
ec2_asg_facts:
tags:
env: "{{ env }}"
project: "{{ project }}"
role: "{{ role }}"
stage: pre
register: new_group
failed_when: new_group.results | length != 1
tags: find
- name: Identify the live stage group
ec2_asg_facts:
tags:
env: "{{ env }}"
project: "{{ project }}"
role: "{{ role }}"
stage: live
register: current_group
failed_when: current_group.results | length > 1
tags: find
- name: Identify the post stage group
ec2_asg_facts:
tags:
env: "{{ env }}"
project: "{{ project }}"
role: "{{ role }}"
stage: post
register: post_group
tags: find
- hosts: localhost
connection: local
tasks:
- name: Terminate any existing post stage group
ec2_asg:
name: "{{ item.auto_scaling_group_name }}"
state: absent
with_items: "{{ post_group.results }}"
- name: Transition pre to live when no existing live group
ec2_asg_target_groups:
name: "{{ new_group.results[0].auto_scaling_group_name }}"
target_groups:
- "{{ project }}-{{ env }}-{{ role }}-live"
when: current_group.results | length == 0
tags: cutover
- name: Perform cutover from pre to live, and live to post
ec2_asg_cutover_target_groups:
current_group_name: "{{ current_group.results[0].auto_scaling_group_name }}"
new_group_name: "{{ new_group.results[0].auto_scaling_group_name }}"
rollback_on_failure: yes
wait_timeout: 300
register: cutover
when: current_group.results | length == 1
tags: cutover
- name: Tag the now live stage group
ec2_asg:
name: "{{ new_group.results[0].auto_scaling_group_name }}"
health_check_type: "{{ new_group.results[0].health_check_type }}" # required to prevent resetting to the default
tags: "{{ new_group.results[0].tags | format_asg_tags | symmetric_difference(new_group.results[0].tags | format_asg_tags | selectattr('stage', 'defined') | list) | union([{'stage': 'live', 'propagate_at_launch': False}, {'deploy_timestamp': date_time_numeric, 'propagate_at_launch': False}]) }}"
- name: Tag the now post stage group
ec2_asg:
name: "{{ current_group.results[0].auto_scaling_group_name }}"
health_check_type: "{{ current_group.results[0].health_check_type }}" # required to prevent resetting to the default
tags: "{{ current_group.results[0].tags | format_asg_tags | symmetric_difference(current_group.results[0].tags | format_asg_tags | selectattr('stage', 'defined') | list) | union([{'stage': 'post', 'propagate_at_launch': False}, {'deploy_timestamp': date_time_numeric, 'propagate_at_launch': False}]) }}"
when: cutover | changed
# vim: set ft=ansible ts=2 sts=2 sw=2 expandtab: