Skip to content

Commit

Permalink
Correctly set path and fullpath for template vars (ansible#73924)
Browse files Browse the repository at this point in the history
* Correctly set path and fullpath for template vars

 don't expect path to always be full path
 also added exception/tb on action fail

(cherry picked from commit 22330dd)
  • Loading branch information
bcoca committed Mar 26, 2021
1 parent 7ef1a2f commit 1da3d40
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/template_temp_vars_fix.yml
@@ -0,0 +1,2 @@
bugfixes:
- Correctly set template_path and template_fullpath for usage in template lookup and action plugins.
3 changes: 2 additions & 1 deletion lib/ansible/errors/__init__.py
Expand Up @@ -20,6 +20,7 @@
__metaclass__ = type

import re
import traceback

from ansible.errors.yaml_strings import (
YAML_COMMON_DICT_ERROR,
Expand Down Expand Up @@ -320,7 +321,7 @@ class AnsibleActionFail(AnsibleAction):
def __init__(self, message="", obj=None, show_content=True, suppress_extended_error=False, orig_exc=None, result=None):
super(AnsibleActionFail, self).__init__(message=message, obj=obj, show_content=show_content,
suppress_extended_error=suppress_extended_error, orig_exc=orig_exc, result=result)
self.result.update({'failed': True, 'msg': message})
self.result.update({'failed': True, 'msg': message, 'exception': traceback.format_exc()})


class _AnsibleActionDone(AnsibleAction):
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/plugins/action/template.py
Expand Up @@ -129,7 +129,7 @@ def run(self, tmp=None, task_vars=None):

# add ansible 'template' vars
temp_vars = task_vars.copy()
temp_vars.update(generate_ansible_template_vars(source, dest))
temp_vars.update(generate_ansible_template_vars(self._task.args.get('src', None), source, dest))

with self._templar.set_temporary_context(searchpath=searchpath, newline_sequence=newline_sequence,
block_start_string=block_start_string, block_end_string=block_end_string,
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/plugins/lookup/template.py
Expand Up @@ -97,7 +97,7 @@ def run(self, terms, variables, **kwargs):
# plus anything passed to the lookup with the template_vars=
# argument.
vars = deepcopy(variables)
vars.update(generate_ansible_template_vars(lookupfile))
vars.update(generate_ansible_template_vars(term, lookupfile))
vars.update(lookup_template_vars)

# do the templating
Expand Down
15 changes: 12 additions & 3 deletions lib/ansible/template/__init__.py
Expand Up @@ -99,8 +99,13 @@
RANGE_TYPE = type(range(0))


def generate_ansible_template_vars(path, dest_path=None):
b_path = to_bytes(path)
def generate_ansible_template_vars(path, fullpath=None, dest_path=None):

if fullpath is None:
b_path = to_bytes(path)
else:
b_path = to_bytes(fullpath)

try:
template_uid = pwd.getpwuid(os.stat(b_path).st_uid).pw_name
except (KeyError, TypeError):
Expand All @@ -111,11 +116,15 @@ def generate_ansible_template_vars(path, dest_path=None):
'template_path': path,
'template_mtime': datetime.datetime.fromtimestamp(os.path.getmtime(b_path)),
'template_uid': to_text(template_uid),
'template_fullpath': os.path.abspath(path),
'template_run_date': datetime.datetime.now(),
'template_destpath': to_native(dest_path) if dest_path else None,
}

if fullpath is None:
temp_vars['template_fullpath'] = os.path.abspath(path)
else:
temp_vars['template_fullpath'] = fullpath

managed_default = C.DEFAULT_MANAGED_STR
managed_str = managed_default.format(
host=temp_vars['template_host'],
Expand Down

0 comments on commit 1da3d40

Please sign in to comment.