├── README └── ansible-ssh /README: -------------------------------------------------------------------------------- 1 | ansible-ssh 2 | ----------- 3 | 4 | This program invokes ssh with the same arguments that ansible does to 5 | connect to hosts defined in its inventory: 6 | 7 | inventory: 8 | foo ansible_ssh_host=x.y.z ansible_ssh_user=foo … 9 | 10 | $ ansible-ssh foo 11 | 12 | Using it saves having to duplicate information from the inventory into 13 | ~/.ssh/config files in order to ssh to them interactively. 14 | 15 | There's an independent ansible-shell program that tries to execute 16 | ansible modules on multiple hosts in parallel. This program has nothing 17 | to do with that. It just lets you ssh to ONE host using information from 18 | the Ansible inventory. 19 | 20 | Note that the "ssh_extra_args" PR against the ansible repository would 21 | make it possible to configure per-host or per-group ProxyCommands to use 22 | jump hosts. We could support that in this program already, but it would 23 | be of limited use until the PR is merged upstream since ansible itself 24 | would not be able to connect to those hosts otherwise. 25 | 26 | https://github.com/ansible/ansible/pull/11908 27 | 28 | The initial prototype of this code was written by Carlos Chapi. It's 29 | still pretty rough around the edges, but the basic functionality works. 30 | 31 | This code is released under the MIT license. 32 | 33 | -- 34 | Abhijit Menon-Sen 35 | -------------------------------------------------------------------------------- /ansible-ssh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # 3 | # This script is an inventory-aware ssh wrapper that eliminates the need 4 | # to repeat information from the inventory in .ssh/config. 5 | 6 | import os 7 | import sys 8 | from ansible.inventory import Inventory 9 | from ansible.parsing import DataLoader 10 | from ansible.vars import VariableManager 11 | from ansible.cli import CLI 12 | 13 | def main(argv): 14 | # We require at least one host pattern on the command line (but we 15 | # may accept other options in future). 16 | if len(argv) < 2: 17 | print "You must specify at least one hostname/pattern" 18 | return -1 19 | 20 | # Basic Ansible initialisation 21 | loader = DataLoader() 22 | variable_manager = VariableManager() 23 | parser = CLI.base_parser( 24 | usage='%prog ', 25 | runtask_opts=True, 26 | ) 27 | options, args = parser.parse_args() 28 | pattern = args[0] 29 | 30 | # Load the inventory and find hosts matching the specified pattern. 31 | inventory = Inventory(loader=loader, variable_manager=variable_manager, host_list=options.inventory) 32 | variable_manager.set_inventory(inventory) 33 | hosts = inventory.list_hosts(pattern) 34 | 35 | # We can't ssh to more than one matching host. 36 | if len(hosts) != 1: 37 | print "Your host pattern matched %d hosts" % len(hosts) 38 | return -1 39 | 40 | # Build ssh arguments for this host 41 | host = hosts[0] 42 | ssh_args = ['ssh'] 43 | 44 | if ('ansible_ssh_private_key_file' in host.vars 45 | and host.vars['ansible_ssh_private_key_file'] is not None): 46 | ssh_args += ("-o", "IdentityFile=\"{0}\"".format(os.path.expanduser(host.vars['ansible_ssh_private_key_file']))) 47 | 48 | if ('ansible_ssh_port' in host.vars 49 | and host.vars['ansible_ssh_port'] is not None): 50 | ssh_args += ("-o", "Port={0}".format(host.vars['ansible_ssh_port'])) 51 | 52 | if ('ansible_ssh_user' in host.vars 53 | and host.vars['ansible_ssh_user'] is not None): 54 | ssh_args += ("-o", "User={0}".format(host.vars['ansible_ssh_user'])) 55 | 56 | ssh_args.append(host.ipv4_address) 57 | 58 | # Launch ssh 59 | os.execl('/usr/bin/ssh', *ssh_args) 60 | 61 | if __name__=='__main__': 62 | main(sys.argv) 63 | --------------------------------------------------------------------------------