Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions library/junos_get_config
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def main():
passwd=dict(required=False, default=None),
port=dict(required=False, default=830),
logfile=dict(required=False, default=None),
dest=dict(required=False, default=None),
dest=dict(required=True, default=None),
format=dict(required=False, choices=['text', 'xml'], default='text'),
options=dict(required=False, default=None),
filter=dict(required=False, default=None)
Expand All @@ -143,6 +143,7 @@ def main():
module.fail_json(msg='junos-eznc >= 1.2.2 is required for this module')

args = module.params
results = {}

logfile = args['logfile']
if logfile is not None:
Expand Down Expand Up @@ -182,11 +183,32 @@ def main():

config = dev.rpc.get_config(options=options, filter_xml=filter_xml)

with open(args['dest'], 'w') as confile:
if args['format'] == 'text':
confile.write(config.text.encode('ascii', 'replace'))
elif args['format'] == 'xml':
confile.write(etree.tostring(config))
## Compare existing file (if exists) with new output
if args['format'] == 'text':
newconf = config.text.encode('ascii', 'replace')
elif args['format'] == 'xml':
newconf = etree.tostring(config)

oldconf = ''
if os.path.isfile(args['dest']):
with open(args['dest'], 'r') as confile:
oldconf = confile.read()

## Correctly report 'changed' attribute
if oldconf != newconf:
results['changed'] = True
if getattr(module,'_diff',False) is True:
results['diff'] = dict(
before = oldconf,
after = newconf,
before_header = args['dest'],
after_header = args['host'],
)

## if check_mode, then don't actually change the dest file!
if not module.check_mode:
with open(args['dest'], 'w') as confile:
confile.write(newconf)

except (ValueError, RpcError) as err:
msg = 'Unable to get config: {0}'.format(str(err))
Expand All @@ -202,7 +224,7 @@ def main():

dev.close()

module.exit_json()
module.exit_json(**results)

from ansible.module_utils.basic import *
main()
6 changes: 5 additions & 1 deletion library/junos_install_config
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ def _load_via_netconf(module):
diff = cu.diff()

if diff is not None:
results['changed'] = True
if getattr(module,'_diff',False) is True:
results['diff'] = dict(
prepared = diff
)
diffs_file = args['diffs_file']
if diffs_file is not None:
try:
Expand Down Expand Up @@ -298,7 +303,6 @@ def _load_via_netconf(module):
opts['confirm'] = args['confirm']

cu.commit(**opts)
results['changed'] = True

except CommitError as err:
msg = "Unable to commit configuration: {0}".format(err)
Expand Down