-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathansible-ssh
executable file
·110 lines (83 loc) · 3.03 KB
/
ansible-ssh
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3.6
from subprocess import call, sys, os
from ansible.parsing.dataloader import DataLoader
from ansible.inventory import Inventory
from ansible.vars import VariableManager
from pprint import pprint
def usage():
prog = os.path.basename(__file__)
sys.stderr.write('Usage: {0} HOSTPATTERN\n'.format(prog))
sys.exit(1)
def get_hosts(pattern):
loader = DataLoader()
variable_manager = VariableManager()
inventory = Inventory(loader=loader, variable_manager=variable_manager)
variable_manager.set_inventory(inventory)
hosts = inventory.get_hosts(pattern=pattern)
for host in hosts:
yield host, variable_manager.get_vars(host=host, loader=loader)
def open_ssh(host, vars, command):
pprint(host)
parameters = ['ssh']
ansible_vars = vars
# ansible_user
# The default ssh user name to use.
username = ansible_vars.get('ansible_user') \
or ansible_vars.get('ansible_ssh_user')
if username is not None:
parameters.append('-l')
parameters.append(username)
# ansible_ssh_private_key_file
# Private key file used by ssh.
# Useful if using multiple keys and you don't want to use SSH agent.
identity_file = ansible_vars.get('ansible_ssh_private_key_file')
if identity_file is not None:
path = os.path.expanduser(identity_file)
path = os.path.expandvars(path)
path = os.path.abspath(path)
parameters.append('-i')
parameters.append(path)
# ansible_port
# The ssh port number, if not 22
port = ansible_vars.get('ansible_port') or 22
if port is not None:
parameters.append('-p')
parameters.append(str(port))
# ansible_host
# The name of the host to connect to, if different from the alias you
# wish to give to it.
hostname = ansible_vars.get('ansible_host') \
or ansible_vars.get('ansible_ssh_host') \
or host.name
# ansible_ssh_common_args
# This setting is always appended to the default command line for
# sftp, scp, and ssh. Useful to configure a ``ProxyCommand`` for a
# certain host (or group).
# ansible_sftp_extra_args
# This setting is always appended to the default sftp command line.
# ansible_scp_extra_args
# This setting is always appended to the default scp command line.
# ansible_ssh_extra_args
# This setting is always appended to the default ssh command line.
parameters.append(hostname)
parameters.extend(command)
print(" ".join(parameters))
call(parameters)
def main(pattern='*', command=[]):
found = False
for host, vars in get_hosts(pattern):
found = True
open_ssh(host, vars, command)
if not found:
sys.stderr.write('No hosts found.\n')
sys.exit(1)
if __name__ == '__main__':
if len(sys.argv) == 1:
usage()
else:
pattern = sys.argv[1]
try:
main(pattern, sys.argv[2:])
except KeyboardInterrupt:
pass
# vim: ts=4 sts=4 et sw=4 ft=python