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
parent 80f0157f3c
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)

View file

@ -116,17 +116,20 @@ RETURN = """
"""
from functools import partial
from ncclient.xml_ import new_ele, sub_ele, to_xml
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.junos import load_config
from ansible.module_utils.six import iteritems
ROLES = ['operator', 'read-only', 'super-user', 'unauthorized']
USE_PERSISTENT_CONNECTION = True
def map_obj_to_ele(want):
element = new_ele('system')
login = sub_ele(element, 'login', {'replace': 'replace'})
element = Element('system')
login = SubElement(element, 'login', {'replace': 'replace'})
for item in want:
if item['state'] != 'present':
@ -134,23 +137,24 @@ def map_obj_to_ele(want):
else:
operation = 'replace'
user = sub_ele(login, 'user', {'operation': operation})
user = SubElement(login, 'user', {'operation': operation})
sub_ele(user, 'name').text = item['name']
SubElement(user, 'name').text = item['name']
if operation == 'replace':
sub_ele(user, 'class').text = item['role']
SubElement(user, 'class').text = item['role']
if item.get('full_name'):
sub_ele(user, 'full-name').text = item['full_name']
SubElement(user, 'full-name').text = item['full_name']
if item.get('sshkey'):
auth = sub_ele(user, 'authentication')
ssh_rsa = sub_ele(auth, 'ssh-rsa')
key = sub_ele(ssh_rsa, 'name').text = item['sshkey']
auth = SubElement(user, 'authentication')
ssh_rsa = SubElement(auth, 'ssh-rsa')
key = SubElement(ssh_rsa, 'name').text = item['sshkey']
return element
def get_param_value(key, item, module):
# if key doesn't exist in the item, get it from module.params
if not item.get(key):
@ -170,6 +174,7 @@ def get_param_value(key, item, module):
return value
def map_params_to_obj(module):
users = module.params['users']
if not users:
@ -229,11 +234,16 @@ def main():
mutually_exclusive = [('users', 'name')]
argument_spec.update(junos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
supports_check_mode=True)
result = {'changed': False}
warnings = list()
check_args(module, warnings)
result = {'changed': False, 'warnings': warnings}
want = map_params_to_obj(module)
ele = map_obj_to_ele(want)
@ -242,7 +252,7 @@ def main():
if module.params['purge']:
kwargs['action'] = 'replace'
diff = load_config(module, ele, **kwargs)
diff = load_config(module, tostring(ele), warnings, **kwargs)
if diff:
result.update({

View file

@ -515,8 +515,6 @@ lib/ansible/modules/network/junos/junos_config.py
lib/ansible/modules/network/junos/junos_facts.py
lib/ansible/modules/network/junos/junos_netconf.py
lib/ansible/modules/network/junos/junos_package.py
lib/ansible/modules/network/junos/junos_rpc.py
lib/ansible/modules/network/junos/junos_user.py
lib/ansible/modules/network/lenovo/cnos_conditional_template.py
lib/ansible/modules/network/lenovo/cnos_template.py
lib/ansible/modules/network/lenovo/cnos_vlan.py