Skip to content

Instantly share code, notes, and snippets.

@jlaska
Created February 7, 2018 19:26
Show Gist options
  • Save jlaska/6b6ef39d639ec59c2412e9302b3ddeae to your computer and use it in GitHub Desktop.
Save jlaska/6b6ef39d639ec59c2412e9302b3ddeae to your computer and use it in GitHub Desktop.
- hosts: localhost
vars:
instance_filters:
- "instance-state-name=running"
instance_excludes:
- "Tags[?Key=='Name' && contains(Value, 'releases.ansible.com')]"
- "Tags[?Key=='galaxy' && Value=='prod']"
uptime: 36
instance_regions:
- 'ap-northeast-1'
- 'ap-northeast-2'
- 'ap-southeast-1'
- 'ap-southeast-2'
- 'eu-central-1'
- 'eu-west-1'
- 'sa-east-1'
- 'us-east-1'
- 'us-east-2'
- 'us-west-1'
- 'us-west-2'
# - 'ap-south-1'
# - 'ca-central-1'
# - 'eu-west-2'
# - 'us-east-2'
tasks:
- name: 'Find stale ec2 instances'
ec2_instance_find:
region: '{{ item }}'
uptime: '{{ uptime }}'
filter: '{{ instance_filters }}'
exclude: '{{ instance_excludes }}'
register: ec2_instances
with_items: '{{ instance_regions }}'
- name: 'Terminate matching instances'
ec2:
region: '{{ item.item }}'
instance_ids: '{{ item.results|map(attribute="InstanceId")|list }}'
state: 'absent'
with_items: '{{ ec2_instances.results }}'
when: '{{ item.results|default([])|length }} > 0 '
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment