Debian Codegraph @

ansible_collections/f5networks/f5_modules/plugins/action/bigip_imish_config.py

raw symbols SCIP
1 #
2 # (c) 2017, Red Hat, Inc.
3 #
4 # This file is part of Ansible
5 #
6 # Ansible is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Ansible is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
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 # User requested backup and no error occurred in module.
51 # NOTE: If there is a parameter error, _backup key may not be in results.
52 filepath = self._write_backup(task_vars['inventory_hostname'], result['__backup__'])
53 result['backup_path'] = filepath
54
55 # strip out any keys that have two leading and two trailing
56 # underscore characters
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 # Create a template search path in the following order:
96 # [working_path, self_role_path, dependent_role_paths, dirname(source)]
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)