Fix backup issue in network config modules and network action plugins common code refactor (#50301)

* Fix backup issue in network config modules

*  Fix `get_working_path` not found issue introduced due to
   backup config code refactor (PR #50208)

*  Further refactor config related action plugins to minimize
   duplicate code

*  Remove unwated imports in config action plugins

* Add common network class for action plugin and related code refactor

* Fix review comment
This commit is contained in:
Ganesh Nalawade 2019-01-04 16:06:13 +05:30 committed by GitHub
parent 8f55e817d2
commit 71113ee291
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 168 additions and 2347 deletions

View file

@ -27,15 +27,14 @@ import re
from ansible.module_utils._text import to_text, to_bytes
from ansible.module_utils.connection import Connection
from ansible.errors import AnsibleError
from ansible.plugins.action import ActionBase
from ansible.plugins.action.network import ActionModule as NetworkActionModule
from ansible.module_utils.six.moves.urllib.parse import urlsplit
from ansible.utils.display import Display
display = Display()
class ActionModule(ActionBase):
class ActionModule(NetworkActionModule):
def run(self, tmp=None, task_vars=None):
changed = True
@ -73,7 +72,7 @@ class ActionModule(ActionBase):
if mode == 'text':
try:
self._handle_template()
self._handle_template(convert_data=False)
except ValueError as exc:
return dict(failed=True, msg=to_text(exc))
@ -182,66 +181,6 @@ class ActionModule(ActionBase):
else:
return True
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 _handle_template(self):
src = self._task.args.get('src')
working_path = self._get_working_path()
if os.path.isabs(src) or urlsplit('src').scheme:
source = src
else:
source = self._loader.path_dwim_relative(working_path, 'templates', src)
if not source:
source = self._loader.path_dwim_relative(working_path, src)
if not os.path.exists(source):
raise ValueError('path specified in src not found')
try:
with open(source, 'r') as f:
template_data = to_text(f.read())
except IOError:
return dict(failed=True, msg='unable to load src file')
# Create a template search path in the following order:
# [working_path, self_role_path, dependent_role_paths, dirname(source)]
searchpath = [working_path]
if self._task._role is not None:
searchpath.append(self._task._role._role_path)
if hasattr(self._task, "_block:"):
dep_chain = self._task._block.get_dep_chain()
if dep_chain is not None:
for role in dep_chain:
searchpath.append(role._role_path)
searchpath.append(os.path.dirname(source))
self._templar.environment.loader.searchpath = searchpath
self._task.args['src'] = self._templar.template(
template_data,
convert_data=False
)
return dict(failed=False, msg='successfully loaded file')
def _get_network_os(self, task_vars):
if 'network_os' in self._task.args and self._task.args['network_os']:
display.vvvv('Getting network OS from task argument')
network_os = self._task.args['network_os']
elif self._play_context.network_os:
display.vvvv('Getting network OS from inventory')
network_os = self._play_context.network_os
elif 'network_os' in task_vars.get('ansible_facts', {}) and task_vars['ansible_facts']['network_os']:
display.vvvv('Getting network OS from fact')
network_os = task_vars['ansible_facts']['network_os']
else:
raise AnsibleError('ansible_network_os must be specified on this host to use platform agnostic modules')
return network_os
def _get_binary_src_file(self, src):
working_path = self._get_working_path()