action plugin net_config will now remove any private result key

This update will now remove any keys from results that are created using
the private names.  Private names are identified as double underscore (__)
on either side of the key name
This commit is contained in:
Peter Sprygada 2016-08-19 10:36:51 -04:00
parent a0a2e1509e
commit d50ef4446f

View file

@ -20,6 +20,7 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import os import os
import re
import time import time
import glob import glob
import urlparse import urlparse
@ -27,6 +28,8 @@ import urlparse
from ansible.plugins.action import ActionBase from ansible.plugins.action import ActionBase
from ansible.utils.unicode import to_unicode from ansible.utils.unicode import to_unicode
PRIVATE_KEYS_RE = re.compile('__.+__')
class ActionModule(ActionBase): class ActionModule(ActionBase):
TRANSFERS_FILES = False TRANSFERS_FILES = False
@ -41,16 +44,24 @@ class ActionModule(ActionBase):
except ValueError as exc: except ValueError as exc:
return dict(failed=True, msg=exc.message) return dict(failed=True, msg=exc.message)
result.update(self._execute_module(module_name=self._task.action, action = self._task.action
result.update(self._execute_module(module_name=action,
module_args=self._task.args, task_vars=task_vars)) module_args=self._task.args, task_vars=task_vars))
if self._task.args.get('backup_config') and result.get('__backup__'): if self._task.args.get('backup') and result.get('__backup__'):
# User requested backup and no error occurred in module. # User requested backup and no error occurred in module.
# NOTE: If there is a parameter error, _backup key may not be in results. # NOTE: If there is a parameter error, _backup key may not be in results.
self._write_backup(task_vars['inventory_hostname'], result['__backup__']) filepath = self._write_backup(task_vars['inventory_hostname'],
result['__backup__'])
result['backup_path'] = filepath
if '__backup__' in result:
del result['__backup__'] # strip out any keys that have two leading and two trailing
# underscore characters
for key in result.keys():
if PRIVATE_KEYS_RE.match(key):
del result[key]
return result return result
@ -69,6 +80,7 @@ class ActionModule(ActionBase):
tstamp = time.strftime("%Y-%m-%d@%H:%M:%S", time.localtime(time.time())) tstamp = time.strftime("%Y-%m-%d@%H:%M:%S", time.localtime(time.time()))
filename = '%s/%s_config.%s' % (backup_path, host, tstamp) filename = '%s/%s_config.%s' % (backup_path, host, tstamp)
open(filename, 'w').write(contents) open(filename, 'w').write(contents)
return filename
def _handle_template(self): def _handle_template(self):
src = self._task.args.get('src') src = self._task.args.get('src')