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>
This commit is contained in:
Radosław Piliszek 2019-07-08 12:31:42 +02:00
parent 768852f8d5
commit 0c00915c41
3 changed files with 26 additions and 23 deletions

View File

@ -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)

View File

@ -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

View File

@ -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.