forked from selivan/ansible-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssh-ansible
67 lines (54 loc) · 2.91 KB
/
ssh-ansible
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/sh
debug=/bin/false
if [ $# -eq 0 ]; then echo "Usage: $(basename $0) hostname <ssh arguments>"; exit 1; fi
if [ "$1"x = "--show"x ]; then ansible-inventory --graph;exit 0; fi
host="$1"
# All other arguments are ssh arguments
shift
for program in ansible-config ansible-inventory; do
which "$program" > /dev/null || { echo "ERROR: not found: $program"; exit 1; }
done
# too slow mybe remove
#ssh_args=$(ansible-config dump | grep '^ANSIBLE_SSH_ARGS' | cut -d'=' -f2-)
#$debug && echo "ssh_args: $ssh_args"
#inventory=$(ansible-inventory --list)
hostvars=$(ansible-inventory -y --host=${host} 2>/dev/null)
if [ $? -ne 0 ] ; then
echo "ERROR: Host ${host} not found!"
echo "You can exec \"ansible-inventory --list |grep -A 5 ${host}\" to checking inventory config."
exit 1;
fi
ansible_host=$(ansible-inventory --graph ${host} 2>/dev/null | sed -n "s/^ *|--//pg")
ansible_host=${ansible_host:-$host}
$debug && echo "ansible_host: $ansible_host"
ansible_port=$(echo $(echo "${hostvars}" | grep -E "^(ansible_port|ansible_ssh_port).*$" | cut -d ':' -f 2))
ansible_user=$(echo $(echo "${hostvars}" | grep -E "^(ansible_user|ansible_ssh_user).*$" | cut -d ':' -f 2))
ansible_ssh_common_args=$(echo $(echo "${hostvars}" | grep -E "^ansible_ssh_common_args.*$" | cut -d ':' -f 2))
ansible_ssh_private_key_file=$(echo $(echo "${hostvars}" | grep -E "^ansible_ssh_private_key_file.*$" | cut -d ':' -f 2))
#all_hosts=$(echo "$inventory" | jq '.[] | .hosts? | .[]? ' | sort | uniq)
#echo "$all_hosts" | grep -q "$host" || { echo "ERROR: $host not found in ansible inventory"; exit 1; }
# Assign new value if variable is not set or empty
# If ansible_host is not set, we should use host name itself
# ansible_host=${ansible_host:-$host}
$debug && echo "ansible_user: $ansible_user"
$debug && echo "ansible_port: $ansible_port"
$debug && echo "ansible_ssh_common_args: $ansible_ssh_common_args"
# We need to prepend hostname with username@
if [ -n "$ansible_user" ]; then ansible_user="${ansible_user}@"; fi
# If port is set, add it to ssh arguments
if [ -n "$ansible_port" ]; then ssh_args="$ssh_args -p $ansible_port"; fi
# If SSH-Agent running,ignore ansible_ssh_private_key_file arguments.
if [ -z "$SSH_AUTH_SOCK" ] ; then
# If identity_file is set, add it to ssh arguments
if [ -n "$ansible_ssh_private_key_file" ]; then sh_args="$ssh_args -i $ansible_ssh_private_key_file"; fi
fi
$debug && echo result command: ssh ${ansible_user}${ansible_host} ${ssh_args} ${ansible_ssh_common_args} $@
exec ssh ${ansible_user}${ansible_host} ${ssh_args} ${ansible_ssh_common_args} $@
if [ $? -ne 0 ]; then
# If SSH-Agent running,ignore ansible_ssh_private_key_file arguments.
if [ -n "$SSH_AUTH_SOCK" ] ; then
$debug || echo result command: ssh ${ansible_user}${ansible_host} ${ssh_args} ${ansible_ssh_common_args} $@
# If identity_file is set, add it to ssh arguments
echo "If ssh auth faild, try exec \"ssh-add $ansible_ssh_private_key_file\""
fi
fi