2014-02-13 15:33:25 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
# (c) 2014, Tomas Karasek <tomas.karasek@digile.fi>
|
|
|
|
#
|
|
|
|
# This file is part of Ansible.
|
|
|
|
#
|
|
|
|
# Ansible is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# Ansible is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
# Dynamic inventory script which lets you use aliases from ~/.ssh/config.
|
|
|
|
#
|
2015-07-29 15:58:16 +02:00
|
|
|
# There were some issues with various Paramiko versions. I took a deeper look
|
|
|
|
# and tested heavily. Now, ansible parses this alright with Paramiko versions
|
|
|
|
# 1.7.2 to 1.15.2.
|
|
|
|
#
|
2014-02-13 15:33:25 +01:00
|
|
|
# It prints inventory based on parsed ~/.ssh/config. You can refer to hosts
|
|
|
|
# with their alias, rather than with the IP or hostname. It takes advantage
|
|
|
|
# of the ansible_ssh_{host,port,user,private_key_file}.
|
|
|
|
#
|
|
|
|
# If you have in your .ssh/config:
|
|
|
|
# Host git
|
|
|
|
# HostName git.domain.org
|
|
|
|
# User tkarasek
|
|
|
|
# IdentityFile /home/tomk/keys/thekey
|
|
|
|
#
|
|
|
|
# You can do
|
|
|
|
# $ ansible git -m ping
|
|
|
|
#
|
|
|
|
# Example invocation:
|
|
|
|
# ssh_config.py --list
|
|
|
|
# ssh_config.py --host <alias>
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import os.path
|
|
|
|
import sys
|
|
|
|
|
2018-08-10 18:13:29 +02:00
|
|
|
import json
|
2014-02-13 15:33:25 +01:00
|
|
|
|
2017-03-26 18:24:30 +02:00
|
|
|
import paramiko
|
|
|
|
|
2018-10-05 10:22:25 +02:00
|
|
|
from ansible.module_utils.common._collections_compat import MutableSequence
|
2017-03-26 18:24:30 +02:00
|
|
|
|
2015-07-29 15:58:16 +02:00
|
|
|
SSH_CONF = '~/.ssh/config'
|
|
|
|
|
2014-02-13 15:33:25 +01:00
|
|
|
_key = 'ssh_config'
|
|
|
|
|
|
|
|
_ssh_to_ansible = [('user', 'ansible_ssh_user'),
|
2017-05-09 23:38:08 +02:00
|
|
|
('hostname', 'ansible_ssh_host'),
|
|
|
|
('identityfile', 'ansible_ssh_private_key_file'),
|
|
|
|
('port', 'ansible_ssh_port')]
|
2014-02-13 15:33:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_config():
|
2015-07-29 15:58:16 +02:00
|
|
|
if not os.path.isfile(os.path.expanduser(SSH_CONF)):
|
|
|
|
return {}
|
|
|
|
with open(os.path.expanduser(SSH_CONF)) as f:
|
2014-02-13 15:33:25 +01:00
|
|
|
cfg = paramiko.SSHConfig()
|
|
|
|
cfg.parse(f)
|
|
|
|
ret_dict = {}
|
|
|
|
for d in cfg._config:
|
2017-03-26 18:24:30 +02:00
|
|
|
if isinstance(d['host'], MutableSequence):
|
2015-07-29 15:58:16 +02:00
|
|
|
alias = d['host'][0]
|
|
|
|
else:
|
|
|
|
alias = d['host']
|
|
|
|
if ('?' in alias) or ('*' in alias):
|
|
|
|
continue
|
2014-02-13 15:33:25 +01:00
|
|
|
_copy = dict(d)
|
|
|
|
del _copy['host']
|
2015-07-29 15:58:16 +02:00
|
|
|
if 'config' in _copy:
|
|
|
|
ret_dict[alias] = _copy['config']
|
|
|
|
else:
|
|
|
|
ret_dict[alias] = _copy
|
2014-02-13 15:33:25 +01:00
|
|
|
return ret_dict
|
|
|
|
|
|
|
|
|
|
|
|
def print_list():
|
|
|
|
cfg = get_config()
|
|
|
|
meta = {'hostvars': {}}
|
|
|
|
for alias, attributes in cfg.items():
|
|
|
|
tmp_dict = {}
|
|
|
|
for ssh_opt, ans_opt in _ssh_to_ansible:
|
|
|
|
if ssh_opt in attributes:
|
2015-07-29 15:58:16 +02:00
|
|
|
# If the attribute is a list, just take the first element.
|
|
|
|
# Private key is returned in a list for some reason.
|
|
|
|
attr = attributes[ssh_opt]
|
2017-03-26 18:24:30 +02:00
|
|
|
if isinstance(attr, MutableSequence):
|
2015-07-29 15:58:16 +02:00
|
|
|
attr = attr[0]
|
|
|
|
tmp_dict[ans_opt] = attr
|
2014-02-13 15:33:25 +01:00
|
|
|
if tmp_dict:
|
|
|
|
meta['hostvars'][alias] = tmp_dict
|
|
|
|
|
2015-08-28 08:18:13 +02:00
|
|
|
print(json.dumps({_key: list(set(meta['hostvars'].keys())), '_meta': meta}))
|
2014-02-13 15:33:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def print_host(host):
|
|
|
|
cfg = get_config()
|
2015-08-28 08:18:13 +02:00
|
|
|
print(json.dumps(cfg[host]))
|
2014-02-13 15:33:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
def get_args(args_list):
|
|
|
|
parser = argparse.ArgumentParser(
|
2017-01-29 08:28:53 +01:00
|
|
|
description='ansible inventory script parsing .ssh/config')
|
2014-02-13 15:33:25 +01:00
|
|
|
mutex_group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
help_list = 'list all hosts from .ssh/config inventory'
|
|
|
|
mutex_group.add_argument('--list', action='store_true', help=help_list)
|
|
|
|
help_host = 'display variables for a host'
|
|
|
|
mutex_group.add_argument('--host', help=help_host)
|
|
|
|
return parser.parse_args(args_list)
|
|
|
|
|
|
|
|
|
|
|
|
def main(args_list):
|
|
|
|
|
|
|
|
args = get_args(args_list)
|
|
|
|
if args.list:
|
|
|
|
print_list()
|
|
|
|
if args.host:
|
|
|
|
print_host(args.host)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv[1:])
|