Last active
June 15, 2017 00:06
-
-
Save visnup/818a86bd7b8e3023a0960768dfde2d33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: "Create Launch Configuration, ELB, Auto-Scaling group and rolling update of AMI" | |
hosts: localhost | |
tags: | |
- aws | |
vars: | |
- AMI: ami-xxxxxx | |
- region: us-east-1 | |
- app: "coolapp" | |
- security_groups: | |
- sg-xxxxx | |
- subnets: | |
- subnet-xxxxx | |
- new_lc: "{{app}}-{{ AMI }}" | |
- min_instances: 2 | |
- desired_instances: 2 | |
- max_instances: 4 | |
- instances_type: t2.small | |
- iam_profile: "your_instance_iam_profile" | |
tasks: | |
- name: Get all the Auto Scaling Groups from awscli | |
shell: aws autoscaling describe-auto-scaling-groups | |
register: asg_output | |
- name: Get all the instances handled by an Auto Scalling Groups from awscli | |
shell: aws autoscaling describe-auto-scaling-instances | |
register: asg_instances_output | |
- name: Initialize the current Launch Configration and Auto Scaling Group to Null | |
set_fact: | |
current_lc: null # this necessary for the first update when the ASG doesn't exist | |
current_asg: null | |
running_instances: 0 | |
- name: Find all the matching Auto Scaling Group | |
set_fact: | |
asg_list: "{{ (asg_output.stdout | |
| from_json)['AutoScalingGroups'] | |
| selectattr('AutoScalingGroupName', 'equalto', app) | |
| list }}" | |
- name: Find current Auto Scaling Group and Launch Configuration | |
set_fact: | |
current_asg: "{{ (asg_list | first)['AutoScalingGroupName'] }}" | |
current_lc: "{{ (asg_list | first)['LaunchConfigurationName'] }}" | |
when: asg_list | length > 0 | |
- name: Check if Launch Configuration needs to be updated | |
set_fact: | |
update_lc: "{{ new_lc != current_lc }}" | |
- name: Get Running instances in the Current Auto Scaling Group | |
set_fact: | |
running_instances: "{{ (asg_instances_output.stdout | |
| from_json)['AutoScalingInstances'] | |
| selectattr('AutoScalingGroupName', 'equalto', current_asg) | |
| list | |
| length }}" | |
when: current_asg and update_lc | |
- name: Update desired instances if more instances are currently running | |
set_fact: | |
desired_instances: "{{ running_instances }}" | |
when: running_instances > desired_instances and update_lc | |
- name: Describe Launch configuration | |
shell: aws autoscaling describe-auto-scaling-groups | |
register: asg_output | |
- name: Create or Update Elastic Load Balancer | |
ec2_elb_lb: | |
region: us-east-1 | |
name: "{{ app }}" | |
state: present | |
connection_draining_timeout: 120 | |
scheme: internal | |
security_group_ids: "{{ security_groups }}" | |
subnets: "{{ subnets }}" | |
listeners: | |
- protocol: http | |
load_balancer_port: 80 | |
instance_port: 80 | |
health_check: | |
ping_protocol: tcp | |
ping_port: 80 | |
response_timeout: 29 | |
interval: 30 | |
unhealthy_threshold: 4 | |
healthy_threshold: 2 | |
- name: Create Launch Configuration | |
ec2_lc: | |
name: "{{ new_lc }}" | |
region: "{{ region }}" | |
image_id: "{{ AMI }}" | |
security_groups: "{{ security_groups }}" | |
instance_type: "{{ instances_type }}" | |
instance_profile_name: "{{ iam_profile }}" | |
when: update_lc | |
- name: Create or Update Auto Scalling Group | |
ec2_asg: | |
name: "{{ app }}" | |
region: "{{ region }}" | |
launch_config_name: "{{ new_lc }}" | |
health_check_period: 300 | |
health_check_type: ELB | |
desired_capacity: "{{ desired_instances }}" | |
min_size: 2 | |
max_size: 10 | |
vpc_zone_identifier: "{{ vpc_subnets.subnets|map(attribute='id')|list }}" | |
load_balancers: "{{ app }}" | |
replace_all_instances: true | |
wait_for_instances: true | |
when: update_lc | |
- name: Delete Old Launch Configuration | |
ec2_lc: | |
name: "{{current_lc}}" | |
state: absent | |
region: "{{ region }}" | |
when: update_lc and current_lc # current_lc is now the old launch config |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment