diff options
| author | Radosław Piliszek <radoslaw.piliszek@gmail.com> | 2019-07-08 12:31:42 +0200 |
|---|---|---|
| committer | Radosław Piliszek <radoslaw.piliszek@gmail.com> | 2019-07-10 20:52:53 +0200 |
| commit | 0c00915c4141c5e3e6dcdaf52148da9f14dbf8f2 (patch) | |
| tree | 2ff91e49ba61f41b05e8d17fc390069dc41f62c9 | |
| parent | 768852f8d5368168e44ef4ffd867fa51e06e0f96 (diff) | |
| download | kolla-ansible-0c00915c4141c5e3e6dcdaf52148da9f14dbf8f2.tar.gz kolla-ansible-0c00915c4141c5e3e6dcdaf52148da9f14dbf8f2.zip | |
Enhance merge_* action plugins to allow expected relative includes
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 <radoslaw.piliszek@gmail.com>
3 files changed, 26 insertions, 23 deletions
diff --git a/ansible/action_plugins/merge_configs.py b/ansible/action_plugins/merge_configs.py index b58d6d7ac..d2c0684bf 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 bf974050c..aa7e9499b 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 000000000..205a7baa5 --- /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. |
