Skip to content
Snippets Groups Projects
Commit 0f672613 authored by Zuul's avatar Zuul Committed by Gerrit Code Review
Browse files

Merge "Enhance merge_* action plugins to allow expected relative includes"

parents 7a2e08d8 0c00915c
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
# limitations under the License. # limitations under the License.
import collections import collections
import inspect
import os import os
import shutil import shutil
import tempfile import tempfile
...@@ -135,6 +134,15 @@ class ActionModule(action.ActionBase): ...@@ -135,6 +134,15 @@ class ActionModule(action.ActionBase):
if os.access(source, os.R_OK): if os.access(source, os.R_OK):
with open(source, 'r') as f: with open(source, 'r') as f:
template_data = f.read() 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) result = self._templar.template(template_data)
fakefile = StringIO(result) fakefile = StringIO(result)
config.parse(fakefile) config.parse(fakefile)
...@@ -143,17 +151,7 @@ class ActionModule(action.ActionBase): ...@@ -143,17 +151,7 @@ class ActionModule(action.ActionBase):
def run(self, tmp=None, task_vars=None): def run(self, tmp=None, task_vars=None):
result = super(ActionModule, self).run(tmp, task_vars) 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)
sources = self._task.args.get('sources', None) sources = self._task.args.get('sources', None)
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
import inspect
import os import os
import shutil import shutil
import tempfile import tempfile
...@@ -80,6 +79,15 @@ class ActionModule(action.ActionBase): ...@@ -80,6 +79,15 @@ class ActionModule(action.ActionBase):
if os.access(source, os.R_OK): if os.access(source, os.R_OK):
with open(source, 'r') as f: with open(source, 'r') as f:
template_data = f.read() 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) template_data = self._templar.template(template_data)
result = safe_load(template_data) result = safe_load(template_data)
return result or {} return result or {}
...@@ -88,17 +96,8 @@ class ActionModule(action.ActionBase): ...@@ -88,17 +96,8 @@ class ActionModule(action.ActionBase):
if task_vars is None: if task_vars is None:
task_vars = dict() task_vars = dict()
result = super(ActionModule, self).run(tmp, task_vars) 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. # save template args.
extra_vars = self._task.args.get('vars', list()) extra_vars = self._task.args.get('vars', list())
old_vars = self._templar._available_variables old_vars = self._templar._available_variables
......
---
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.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment