#!/usr/bin/env python
# -h for usage
# translates from standard ssh arguments into an ansible playbook host line
# still pretty basic, no error handling etc

# example 1 > user@host -p 22 -i /path/to/key
# example 2 > host -l user -i /path/to/key

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("host")
parser.add_argument("-l","--user")
parser.add_argument("-p","--port", type=int)
parser.add_argument("-i","--keyfile")
args = parser.parse_args()

hostsplit = args.host.split("@")
if (len(hostsplit)==2):
  user = hostsplit[0]
  host = hostsplit[1]
else:
  user = args.user
  host = args.host

line =  host + " ansible_connection=ssh"
if (user): line = line + " ansible_ssh_user="+user
if (args.keyfile): line = line + " ansible_ssh_private_key_file="+args.keyfile
if (args.port): line = line + " ansible_ssh_port="+str(args.port)

print line
