#!/usr/bin/python

from Connect import Connect
import logging
import time
import re
import os
from ansible.module_utils.basic import *

module = AnsibleModule(
	argument_spec 	= dict(
	host 		= dict(required=True)
	),
	supports_check_mode = False
)

host = module.params['host']

c = Connect(host)
time.sleep(1)
stdin,stdout,stderr = c.ssh.exec_command('show version\n')
time.sleep(5)

output = stdout.read()

c.hostname = re.search(r'(.+) uptime is', output).group(1).strip()
c.software = re.search(r'Cisco (.+) Software,', output).group(1)
if c.software == "IOS XR":
    c.version = re.search(r'Version (\d.\d.\d)', output).group(1)
else:
    c.version = re.search(r'Version (.+?),', output).group(1)
if c.software == "IOS XR":
    c.chassis = re.search(r'(.+) Chassis', output).group(1)
else:
    c.chassis = re.search(r'cisco (.+) \(', output).group(1).split()[0]
c.uptime = re.search(r'(.+) uptime is (.+)', output).group(2)

facts = dict()
facts['host'] = host
facts['hostname'] = getattr(c,'hostname')
facts['software'] = getattr(c,'software')
facts['version'] = getattr(c,'version')
facts['chassis'] = getattr(c,'chassis')
facts['uptime'] = getattr(c,'uptime')

module.exit_json(**facts)

