Fix junos_rpc and junos_user broken issues (#24238)

* Fix junos_rpc and junos_user broken issues

Add persistent connection related changes.

* Fix CI issues
This commit is contained in:
Ganesh Nalawade 2017-05-03 19:00:07 +05:30 committed by GitHub
commit a42b892f70
3 changed files with 39 additions and 27 deletions

View file

@ -84,12 +84,14 @@ output_lines:
returned: always
type: list
"""
from ncclient.xml_ import new_ele, sub_ele, to_xml, to_ele
from xml.etree.ElementTree import Element, SubElement, tostring
from ansible.module_utils.junos import junos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netconf import send_request
from ansible.module_utils.six import iteritems
USE_PERSISTENT_CONNECTION = True
def main():
@ -101,10 +103,15 @@ def main():
output=dict(default='xml', choices=['xml', 'json', 'text']),
)
argument_spec.update(junos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=False)
result = {'changed': False}
warnings = list()
check_args(module, warnings)
result = {'changed': False, 'warnings': warnings}
rpc = str(module.params['rpc']).replace('_', '-')
@ -115,37 +122,34 @@ def main():
xattrs = {'format': module.params['output']}
element = new_ele(module.params['rpc'], xattrs)
element = Element(module.params['rpc'], xattrs)
for key, value in iteritems(args):
key = str(key).replace('_', '-')
if isinstance(value, list):
for item in value:
child = sub_ele(element, key)
child = SubElement(element, key)
if item is not True:
child.text = item
else:
child = sub_ele(element, key)
child = SubElement(element, key)
if value is not True:
child.text = value
reply = send_request(module, element)
result['xml'] = str(to_xml(reply))
result['xml'] = str(tostring(reply))
if module.params['output'] == 'text':
reply = to_ele(reply)
data = reply.xpath('//output')
result['output'] = data[0].text.strip()
data = reply.find('.//output')
result['output'] = data.text.strip()
result['output_lines'] = result['output'].split('\n')
elif module.params['output'] == 'json':
reply = to_ele(reply)
data = reply.xpath('//rpc-reply')
result['output'] = module.from_json(data[0].text.strip())
result['output'] = module.from_json(reply.text.strip())
else:
result['output'] = str(to_xml(reply)).split('\n')
result['output'] = str(tostring(reply)).split('\n')
module.exit_json(**result)