1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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

View file

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