Junos fixes (#22423)

* Fixes for junos_config errors

* Check transport settings for core Junos

* Don't pop from the same list you iterate over

* use of persistent connections are now explicitly enabled in junos

* modules must now explicitly enable persistent connections
* adds rpc support to junos_command

fixes #22166
This commit is contained in:
Peter Sprygada
2017-03-11 10:26:42 -06:00
committed by GitHub
parent 17fc6832ca
commit 1825406e1e
7 changed files with 207 additions and 86 deletions

View File

@@ -18,7 +18,7 @@
#
from contextlib import contextmanager
from ncclient.xml_ import new_ele, sub_ele, to_xml
from xml.etree.ElementTree import Element, SubElement, tostring
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.netconf import send_request
@@ -41,6 +41,7 @@ junos_argument_spec = {
'ssh_keyfile': dict(fallback=(env_fallback, ['ANSIBLE_NET_SSH_KEYFILE']), type='path'),
'timeout': dict(type='int', default=10),
'provider': dict(type='dict'),
'transport': dict(choices=['cli', 'netconf'])
}
def check_args(module, warnings):
@@ -81,17 +82,17 @@ def load_configuration(module, candidate=None, action='merge', rollback=None, fo
else:
xattrs = {'action': action, 'format': format}
obj = new_ele('load-configuration', xattrs)
obj = Element('load-configuration', xattrs)
if candidate is not None:
lookup = {'xml': 'configuration', 'text': 'configuration-text',
'set': 'configuration-set', 'json': 'configuration-json'}
if action == 'set':
cfg = sub_ele(obj, 'configuration-set')
cfg = SubElement(obj, 'configuration-set')
cfg.text = '\n'.join(candidate)
else:
cfg = sub_ele(obj, lookup[format])
cfg = SubElement(obj, lookup[format])
cfg.append(candidate)
return send_request(module, obj)
@@ -104,22 +105,22 @@ def get_configuration(module, compare=False, format='xml', rollback='0'):
validate_rollback_id(rollback)
xattrs['compare'] = 'rollback'
xattrs['rollback'] = str(rollback)
return send_request(module, new_ele('get-configuration', xattrs))
return send_request(module, Element('get-configuration', xattrs))
def commit_configuration(module, confirm=False, check=False, comment=None, confirm_timeout=None):
obj = new_ele('commit-configuration')
obj = Element('commit-configuration')
if confirm:
sub_ele(obj, 'confirmed')
SubElement(obj, 'confirmed')
if check:
sub_ele(obj, 'check')
SubElement(obj, 'check')
if comment:
children(obj, ('log', str(comment)))
if confirm_timeout:
children(obj, ('confirm-timeout', int(confirm_timeout)))
return send_request(module, obj)
lock_configuration = lambda x: send_request(x, new_ele('lock-configuration'))
unlock_configuration = lambda x: send_request(x, new_ele('unlock-configuration'))
lock_configuration = lambda x: send_request(x, Element('lock-configuration'))
unlock_configuration = lambda x: send_request(x, Element('unlock-configuration'))
@contextmanager
def locked_config(module):
@@ -131,7 +132,7 @@ def locked_config(module):
def get_diff(module):
reply = get_configuration(module, compare=True, format='text')
output = reply.xpath('//configuration-output')
output = reply.find('.//configuration-output')
if output:
return output[0].text

View File

@@ -26,50 +26,50 @@
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
from contextlib import contextmanager
from ncclient.xml_ import new_ele, sub_ele, to_xml, to_ele
from xml.etree.ElementTree import Element, SubElement
from xml.etree.ElementTree import tostring, fromstring
from ansible.module_utils.connection import exec_command
def send_request(module, obj, check_rc=True):
request = to_xml(obj)
request = tostring(obj)
rc, out, err = exec_command(module, request)
if rc != 0:
if check_rc:
module.fail_json(msg=str(err))
return to_ele(err)
return to_ele(out)
return fromstring(out)
return fromstring(out)
def children(root, iterable):
for item in iterable:
try:
ele = sub_ele(ele, item)
ele = SubElement(ele, item)
except NameError:
ele = sub_ele(root, item)
ele = SubElement(root, item)
def lock(module, target='candidate'):
obj = new_ele('lock')
obj = Element('lock')
children(obj, ('target', target))
return send_request(module, obj)
def unlock(module, target='candidate'):
obj = new_ele('unlock')
obj = Element('unlock')
children(obj, ('target', target))
return send_request(module, obj)
def commit(module):
return send_request(module, new_ele('commit'))
return send_request(module, Element('commit'))
def discard_changes(module):
return send_request(module, new_ele('discard-changes'))
return send_request(module, Element('discard-changes'))
def validate(module):
obj = new_ele('validate')
obj = Element('validate')
children(obj, ('source', 'candidate'))
return send_request(module, obj)
def get_config(module, source='running', filter=None):
obj = new_ele('get-config')
obj = Element('get-config')
children(obj, ('source', source))
children(obj, ('filter', filter))
return send_request(module, obj)