Skip to content

Instantly share code, notes, and snippets.

@luckylittle
Last active May 16, 2020 23:57
Show Gist options
  • Select an option

  • Save luckylittle/d01c88cc71140a4620dc7e783ef8a637 to your computer and use it in GitHub Desktop.

Select an option

Save luckylittle/d01c88cc71140a4620dc7e783ef8a637 to your computer and use it in GitHub Desktop.
Red Hat Ansible - Creation and cleanup of AWS AMIs
---
# Tested on Ansible 2.9.4
# ansible-playbook -i localhost, -e "ami_backup_ec2_name=sandbox-dev-b" -e "ami_backup_count=3" -e "ami_backup_remove=yes" -e "ami_backup_ec2_region=ap-southeast-2" playbook-backup_ami_role.yml
# Copyright (c) 2020 Lucian Maly, Red Hat
- name: 1.0 | BACKUP_AMI | Gathering information about the instance {{ ami_backup_ec2_name }}.
ec2_instance_info:
filters:
"tag:Name": "{{ ami_backup_ec2_name }}"
region: "{{ ami_backup_ec2_region }}"
register: ec2
tags:
- backup
- ami
- "1"
- name: 1.1 | BACKUP_AMI | Extracting the tags of the instance {{ ami_backup_ec2_name }}.
set_fact:
"{{ 'ami_backup_tags_'+item.key }}": "{{ item.value }}"
loop: "{{ ec2.instances.0.tags | dict2items }}"
tags:
- backup
- ami
- "1"
- name: 1.2 | BACKUP_AMI | Finding AMI(s) for the instance {{ ami_backup_ec2_name }}.
ec2_ami_info:
ec2_region: "{{ ami_backup_ec2_region }}"
filters:
"tag:Name": "{{ ami_backup_ec2_name }}"
register: fact_ami_info
tags:
- backup
- ami
- "1"
- name: 2.0 | BACKUP_AMI | Setting value of 'Increment' if custom AMI already exists.
set_fact:
fact_increment: "{{ fact_ami_info.images[-1].tags.Increment | int + 1 }}"
when: fact_ami_info.images.0 is defined
tags:
- backup
- ami
- "2"
- name: 2.1 | BACKUP_AMI | Setting value of 'Increment' if custom AMI does not exist.
set_fact:
fact_increment: 01
when: fact_ami_info.images.0 is not defined
tags:
- backup
- ami
- "2"
- name: 3.0 | BACKUP_AMI | Creating AMI of {{ ami_backup_ec2_name }}.
ec2_ami:
state: present
region: "{{ ami_backup_ec2_region }}"
instance_id: "{{ ec2.instances[-1].instance_id }}"
name: "{{ ami_backup_ec2_name }}-{{ fact_increment }}"
tags: # these tags must exist on the instance
Name: "{{ ami_backup_ec2_name }}"
Environment: "{{ ami_backup_tags_Environment }}"
Solution: "{{ ami_backup_tags_Solution }}"
TierArchetype: "{{ ami_backup_tags_TierArchetype }}"
TierType: "{{ ami_backup_tags_TierType }}"
Increment: "{{ fact_increment }}"
wait: yes
no_reboot: yes
wait_timeout: 900 # some of my AMIs take up to 25 minutes
register: image_created
tags:
- backup
- ami
- "3"
- name: 3.1 | BACKUP_AMI | AMI was created.
debug:
msg: "#> AMI created NAME={{ ami_backup_ec2_name }}-{{ fact_increment }} ID={{ image_created.image_id }}"
when: image_created is defined
tags:
- backup
- ami
- "3"
- name: 4.0 | BACKUP_AMI | Reverse the list of AMIs so the oldest ones get removed.
set_fact:
reverse_list: "{{ fact_ami_info.images | reverse | list }}"
tags:
- backup
- ami
- "4"
- name: 4.1 | BACKUP_AMI | Deleting the oldest number of AMI's that exceed the specified COUNT={{ ami_backup_count }}.
ec2_ami:
state: absent
region: "{{ ami_backup_ec2_region }}"
image_id: "{{ item.image_id }}"
wait_timeout: 500
with_items: "{{ reverse_list[ami_backup_count | int - 1:] }}"
when: "reverse_list[ami_backup_count | int - 1:] | length > 0 and ami_backup_remove"
register: image_deleted
tags:
- backup
- ami
- "4"
- name: 4.2 | BACKUP_AMI | AMI is deleted.
debug:
msg: "#> AMI deleted NAME={{ item.name }} ID={{ item.image_id }}"
with_items: "{{ reverse_list[ami_backup_count | int - 1:] }}"
when: "image_deleted is defined and reverse_list[ami_backup_count | int - 1:] | length > 0 and ami_backup_remove"
tags:
- backup
- ami
- "4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment