Skip to content

Instantly share code, notes, and snippets.

@harishbsrinivas
Last active September 15, 2015 06:14
Show Gist options
  • Save harishbsrinivas/f8b744f5136bb354cc85 to your computer and use it in GitHub Desktop.
Save harishbsrinivas/f8b744f5136bb354cc85 to your computer and use it in GitHub Desktop.
This script uses ansible EC2 invetory script to list all security groups currently in use. This can be used to clean up unused SGs
#!/usr/bin/env bash
set -e
set -u
# This script expects that ansible, ansible EC2 invetory script are already installed and setup correctly.
# The EC2 invetory script should be named /etc/ansible/hosts.
if hash ansible 2>/dev/null; then
if [[ ! -f /etc/hosts/ansible ]]; then
/etc/ansible/hosts > /dev/null
echo ""
echo "Security groups in use."
echo "-------------------------------------------------------------------------"
grep security_group_names < $HOME/.ansible/tmp/ansible-ec2.cache | sort | uniq | awk '{ print $2 }' | awk -F "\"" '{ print $2 }' | sort -i
else
echo "EC2 inventory script was not found in /etc/ansible/hosts"
exit 1
fi
else
echo "Ansible is not installed!!"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment