Last active
December 15, 2015 19:19
-
-
Save kevinbringard/5310718 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env bash | |
# We need to cat lines with spaces | |
OLD_IFS=$IFS | |
IFS=$'\n' | |
OUTFILE="hosts.file" | |
NOVA_CREDS="" | |
# If the credentials file isn't specified we exit | |
if [ -z $NOVA_CREDS ]; then | |
echo "You must provide the location of your OpenStack credential file NOVA_CREDS" | |
exit 1 | |
fi | |
# Source our creds file | |
. $NOVA_CREDS | |
# Create a header with what and when we were created | |
echo "# Created by $0 at $( date )" > $OUTFILE | |
# Cache our list so we can iterate on it | |
nova list >> $$.tmp | |
# Loop over all our instances and create a hosts file out of them | |
for i in $( cat $$.tmp | grep -v "+---" | grep -v Networks ); do | |
hostname=$( echo $i | awk -F'|' '{ print $3 }' | sed 's/ //g' ) | |
ip=$( echo $i | awk -F'|' '{ print $5 }' | awk -F= '{ print $2 }' | awk -F, '{ print $1 }' | sed 's/ //g' ) | |
if [ -z $ip ]; then | |
continue | |
fi | |
# Save the hosts file somewhere, based on $OUTFILE above | |
echo "$ip $hostname" >> $OUTFILE | |
done | |
# Delete our tmp file | |
rm -f $$.tmp | |
# Set IFS back to what it was before we started | |
IFS=$old_IFS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment