| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import (absolute_import, division, print_function) |
| 20 |
__metaclass__ = type |
| 21 |
|
| 22 |
import os |
| 23 |
import re |
| 24 |
import time |
| 25 |
import glob |
| 26 |
|
| 27 |
from ansible.module_utils._text import to_text |
| 28 |
from ansible.module_utils.six.moves.urllib.parse import urlsplit |
| 29 |
from ansible_collections.ansible.netcommon.plugins.action.network import ActionModule as ActionNetworkModule |
| 30 |
from ansible.utils.display import Display |
| 31 |
|
| 32 |
display = Display() |
| 33 |
|
| 34 |
|
| 35 |
PRIVATE_KEYS_RE = re.compile('__.+__') |
| 36 |
|
| 37 |
|
| 38 |
class ActionModule(ActionNetworkModule): |
| 39 |
|
| 40 |
def run(self, task_vars=None): |
| 41 |
if self._task.args.get('src'): |
| 42 |
try: |
| 43 |
self._handle_template() |
| 44 |
except ValueError as exc: |
| 45 |
return dict(failed=True, msg=to_text(exc)) |
| 46 |
|
| 47 |
result = super(ActionModule, self).run(task_vars=task_vars) |
| 48 |
|
| 49 |
if self._task.args.get('backup') and result.get('__backup__'): |
| 50 |
|
| 51 |
|
| 52 |
filepath = self._write_backup(task_vars['inventory_hostname'], result['__backup__']) |
| 53 |
result['backup_path'] = filepath |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
for key in list(result.keys()): |
| 58 |
if PRIVATE_KEYS_RE.match(key): |
| 59 |
del result[key] |
| 60 |
return result |
| 61 |
|
| 62 |
def _write_backup(self, host, contents): |
| 63 |
backup_path = self._get_working_path() + '/backup' |
| 64 |
if not os.path.exists(backup_path): |
| 65 |
os.mkdir(backup_path) |
| 66 |
for fn in glob.glob('%s/%s*' % (backup_path, host)): |
| 67 |
os.remove(fn) |
| 68 |
tstamp = time.strftime("%Y-%m-%d@%H:%M:%S", time.localtime(time.time())) |
| 69 |
filename = '%s/%s_config.%s' % (backup_path, host, tstamp) |
| 70 |
fh = open(filename, 'w') |
| 71 |
fh.write(contents) |
| 72 |
fh.close() |
| 73 |
return filename |
| 74 |
|
| 75 |
def _handle_template(self): |
| 76 |
src = self._task.args.get('src') |
| 77 |
working_path = self._get_working_path() |
| 78 |
|
| 79 |
if os.path.isabs(src) or urlsplit('src').scheme: |
| 80 |
source = src |
| 81 |
else: |
| 82 |
source = self._loader.path_dwim_relative(working_path, 'templates', src) |
| 83 |
if not source: |
| 84 |
source = self._loader.path_dwim_relative(working_path, src) |
| 85 |
|
| 86 |
if not os.path.exists(source): |
| 87 |
raise ValueError('path specified in src not found') |
| 88 |
|
| 89 |
try: |
| 90 |
with open(source, 'r') as f: |
| 91 |
template_data = to_text(f.read()) |
| 92 |
except IOError: |
| 93 |
return dict(failed=True, msg='unable to load src file') |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
searchpath = [working_path] |
| 98 |
if self._task._role is not None: |
| 99 |
searchpath.append(self._task._role._role_path) |
| 100 |
if hasattr(self._task, "_block:"): |
| 101 |
dep_chain = self._task._block.get_dep_chain() |
| 102 |
if dep_chain is not None: |
| 103 |
for role in dep_chain: |
| 104 |
searchpath.append(role._role_path) |
| 105 |
searchpath.append(os.path.dirname(source)) |
| 106 |
self._templar.environment.loader.searchpath = searchpath |
| 107 |
self._task.args['src'] = self._templar.template(template_data) |