Last active
May 22, 2018 19:04
-
-
Save cryptiklemur/df1ea42fb0b7bb7ddeb32636f6a1d88c to your computer and use it in GitHub Desktop.
Allows for SSHing (with tunnels) to any of the ansible inventories
This file contains 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
#!/bin/bash | |
directory=~/projects/php-nginx-wp-ansible | |
if [[ "$2" != "dbservers" && "$2" != "webservers" ]]; then | |
echo "" | |
echo "$2 is not a valid type. Pass either dbservers or webservers" | |
exit | |
fi | |
ips=$(ansible-inventory -i $directory/inventories/$1 $2 --graph | grep "|--" | sed "s/ |--//g" | tr '\n' ' ' ) | |
IFS=' ' read -r -a array <<< "$ips" | |
if [[ ${#array[@]} > 1 && -z ${3+x} ]]; then | |
echo "" | |
for key in "${!array[@]}"; do | |
printf "%-8s\n" "${key}) ${array[$key]}" | |
done | |
echo "" | |
echo "Select an index to ssh to (0-indexed): " | |
read $3 | |
fi | |
ip="${array[$3]}" | |
if [[ -z "$TUNNEL" ]]; then | |
echo "SSHing to ${ip}" | |
echo "" | |
sleep 1 | |
clear | |
ssh git@$ip | |
else | |
echo "" | |
echo "Tunneling to ${ip} on port ${TUNNEL}..." | |
ssh -N -S none -L $TUNNEL:127.0.0.1:$TUNNEL git@$ip | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
$ afssh production_nl webserver $ TUNNEL=3306 afssh production_nl dbservers $ afssh production_us webservers # Will show a list of IPs you can ssh to