Created
February 24, 2015 14:50
-
-
Save joergrathlev/a697b7d74de8a404e81a to your computer and use it in GitHub Desktop.
Convenience Bash function to connect to a server by its Ansible name
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
function ssha () { | |
if [ $# -eq 0 ] | |
then | |
echo "Usage: ssha <ansible-hostname> [ssh arguments]" >&2 | |
return 1 | |
fi | |
local inventory=/path/to/your/inventory # CHANGE THIS LINE | |
local ansible_name="$1" | |
shift | |
local host=$(ansible -i "$inventory" --list-hosts "$ansible_name" | head -n 1) | |
if [ -z "$host" ] | |
then | |
return 1 | |
else | |
# Arghh, leading whitespace in Ansible's output | |
host="${host#"${host%%[![:space:]]*}"}" | |
ssh "$host" "$@" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Better use ansible configuration instead of inventory file, because there can be more than one inventory file: