Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev find untagged #332

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions site.maintenance.find-untagged.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
- hosts: localhost
connection: local
vars:
_ec2_filters:
- "running"
- "stopped"
- "pending"
- "shutting-down"
- "stopping"
_ec2_ebs_list_to_review: []
_ec2_untagged_list: []
_ec2_list_to_review :
roles:
- role: cs.aws-rds-facts
tasks:
- name: List of mageops instances
ec2_instance_info:
filters:
"tag:Infrastructure": 'mageops'
"tag:Tool": 'ansible'
instance-state-name: "{{ _ec2_filters }}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is much more states than "running" and "stopped", ideally we would want to catch all of them
I would change it to not filter it here, instead later one ignore all that are in "terminated" state

region: "{{ aws_region }}"
register: _ec2_list_mageops

- name: List of all EC2 instances
ec2_instance_info:
filters:
instance-state-name: "{{ _ec2_filters }}"
region: "{{ aws_region }}"
register: _ec2_list_all

- name: Generate difference between both list
set_fact:
_ec2_untagged_list: "{{ _ec2_untagged_list + [{ 'id' : item.instance_id, 'name' : item.tags.Name | default('') }] }}"
when: item.instance_id not in aws_ec2_whitelist
with_items: "{{ _ec2_list_all.instances | difference(_ec2_list_mageops.instances) }}"

- name: Find unused EBS volumes
ec2_vol_info:
filters:
status: "available"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is clever way to extract it 👍

region: "{{ aws_region }}"
register: _ec2_ebs_list

- name: Set unused EBS volumes list
set_fact:
_ec2_ebs_list_to_review: "{{ _ec2_ebs_list_to_review + [item.id] }}"
with_items: "{{ _ec2_ebs_list.volumes }}"

- name: Generate string to EC2 end output
set_fact:
_ec2_list_to_review: "{{ (_ec2_list_to_review) }} ID : {{ item.id }} Name : {{ item.name }}"
with_items: "{{ _ec2_untagged_list }}"

- name: Failed when some resources found
fail:
msg: |
{% if (_ec2_ebs_list.volumes | length) > 0 %} EBS volumes to review: {{ _ec2_ebs_list_to_review | join(',') }} {% endif %}
{% if (_ec2_untagged_list | length) > 0 %} EC2 instances to review:
{{ _ec2_list_to_review }} {% endif %}
when: "{{ (_ec2_untagged_list | length > 0) or (_ec2_ebs_list.volumes | length >0) }}"