Merge backup functionality across config action plugins (#50208)

* Merge backup functionality across config action plugins

* Port updated cli_config
This commit is contained in:
Nathaniel Case 2018-12-21 10:30:33 -05:00 committed by GitHub
parent 0b3aa75b7f
commit c093ea5a28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 103 additions and 577 deletions

View file

@ -19,12 +19,11 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import glob
import os
import re
import time
from ansible.plugins.action.normal import ActionModule as _ActionModule
from ansible.module_utils.network.common.backup import write_backup
PRIVATE_KEYS_RE = re.compile('__.+__')
@ -41,9 +40,7 @@ class ActionModule(_ActionModule):
if self._task.args.get('backup') and result.get('__backup__'):
# User requested backup and no error occurred in module.
# NOTE: If there is a parameter error, _backup key may not be in results.
filepath = self._write_backup(task_vars['inventory_hostname'],
result['__backup__'])
filepath = write_backup(self, task_vars['inventory_hostname'], result['__backup__'])
result['backup_path'] = filepath
# strip out any keys that have two leading and two trailing
@ -53,20 +50,3 @@ class ActionModule(_ActionModule):
del result[key]
return result
def _get_working_path(self):
cwd = self._loader.get_basedir()
if self._task._role is not None:
cwd = self._task._role._role_path
return cwd
def _write_backup(self, host, contents):
backup_path = self._get_working_path() + '/backup'
if not os.path.exists(backup_path):
os.mkdir(backup_path)
for existing_backup in glob.glob('%s/%s_config.*' % (backup_path, host)):
os.remove(existing_backup)
tstamp = time.strftime("%Y-%m-%d@%H:%M:%S", time.localtime(time.time()))
filename = '%s/%s_config.%s' % (backup_path, host, tstamp)
open(filename, 'w').write(contents)
return filename