From 0c00915c4141c5e3e6dcdaf52148da9f14dbf8f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rados=C5=82aw=20Piliszek?= Date: Mon, 8 Jul 2019 12:31:42 +0200 Subject: [PATCH] Enhance merge_* action plugins to allow expected relative includes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This mimics behavior of core 'template' module to allow relative includes from the same dir as merged template, base dir of playbook/role (usually role for us) and its 'templates' subdir. Additionally old unused code was removed. Change-Id: I83804d3cf5f17eb2302a2dfe49229c6277b1e25f Signed-off-by: Radosław Piliszek --- ansible/action_plugins/merge_configs.py | 22 +++++++++---------- ansible/action_plugins/merge_yaml.py | 21 +++++++++--------- ...th-relative-includes-bd0a16cce82707d1.yaml | 6 +++++ 3 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 releasenotes/notes/enhance-merge-action-plugins-with-relative-includes-bd0a16cce82707d1.yaml diff --git a/ansible/action_plugins/merge_configs.py b/ansible/action_plugins/merge_configs.py index b58d6d7acf..d2c0684bfc 100644 --- a/ansible/action_plugins/merge_configs.py +++ b/ansible/action_plugins/merge_configs.py @@ -16,7 +16,6 @@ # limitations under the License. import collections -import inspect import os import shutil import tempfile @@ -135,6 +134,15 @@ class ActionModule(action.ActionBase): if os.access(source, os.R_OK): with open(source, 'r') as f: template_data = f.read() + + # set search path to mimic 'template' module behavior + searchpath = [ + self._loader._basedir, + os.path.join(self._loader._basedir, 'templates'), + os.path.dirname(source), + ] + self._templar.environment.loader.searchpath = searchpath + result = self._templar.template(template_data) fakefile = StringIO(result) config.parse(fakefile) @@ -143,17 +151,7 @@ class ActionModule(action.ActionBase): def run(self, tmp=None, task_vars=None): result = super(ActionModule, self).run(tmp, task_vars) - - # NOTE(jeffrey4l): Ansible 2.1 add a remote_user param to the - # _make_tmp_path function. inspect the number of the args here. In - # this way, ansible 2.0 and ansible 2.1 are both supported - make_tmp_path_args = inspect.getargspec(self._make_tmp_path)[0] - if not tmp and len(make_tmp_path_args) == 1: - tmp = self._make_tmp_path() - if not tmp and len(make_tmp_path_args) == 2: - remote_user = (task_vars.get('ansible_user') - or self._play_context.remote_user) - tmp = self._make_tmp_path(remote_user) + del tmp # not used sources = self._task.args.get('sources', None) diff --git a/ansible/action_plugins/merge_yaml.py b/ansible/action_plugins/merge_yaml.py index bf974050cd..aa7e9499b1 100755 --- a/ansible/action_plugins/merge_yaml.py +++ b/ansible/action_plugins/merge_yaml.py @@ -15,7 +15,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -import inspect import os import shutil import tempfile @@ -80,6 +79,15 @@ class ActionModule(action.ActionBase): if os.access(source, os.R_OK): with open(source, 'r') as f: template_data = f.read() + + # set search path to mimic 'template' module behavior + searchpath = [ + self._loader._basedir, + os.path.join(self._loader._basedir, 'templates'), + os.path.dirname(source), + ] + self._templar.environment.loader.searchpath = searchpath + template_data = self._templar.template(template_data) result = safe_load(template_data) return result or {} @@ -88,17 +96,8 @@ class ActionModule(action.ActionBase): if task_vars is None: task_vars = dict() result = super(ActionModule, self).run(tmp, task_vars) + del tmp # not used - # NOTE(jeffrey4l): Ansible 2.1 add a remote_user param to the - # _make_tmp_path function. inspect the number of the args here. In - # this way, ansible 2.0 and ansible 2.1 are both supported - make_tmp_path_args = inspect.getargspec(self._make_tmp_path)[0] - if not tmp and len(make_tmp_path_args) == 1: - tmp = self._make_tmp_path() - if not tmp and len(make_tmp_path_args) == 2: - remote_user = (task_vars.get('ansible_user') - or self._play_context.remote_user) - tmp = self._make_tmp_path(remote_user) # save template args. extra_vars = self._task.args.get('vars', list()) old_vars = self._templar._available_variables diff --git a/releasenotes/notes/enhance-merge-action-plugins-with-relative-includes-bd0a16cce82707d1.yaml b/releasenotes/notes/enhance-merge-action-plugins-with-relative-includes-bd0a16cce82707d1.yaml new file mode 100644 index 0000000000..205a7baa51 --- /dev/null +++ b/releasenotes/notes/enhance-merge-action-plugins-with-relative-includes-bd0a16cce82707d1.yaml @@ -0,0 +1,6 @@ +--- +features: + - | + Merge action plugins (for config/ini and yaml files) now allow relative + imports in the same way that upstream template modules does, e.g. one can + now include subtemplate from the same directory as base template.