diff --git a/lib/ansible/plugins/action/template.py b/lib/ansible/plugins/action/template.py index 54520b2f7e..b8346cb6f9 100644 --- a/lib/ansible/plugins/action/template.py +++ b/lib/ansible/plugins/action/template.py @@ -18,10 +18,14 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type import base64 +import datetime import os +import time +from ansible import constants as C from ansible.plugins.action import ActionBase from ansible.utils.hashing import checksum_s +from ansible.utils.unicode import to_bytes class ActionModule(ActionBase): @@ -97,7 +101,35 @@ class ActionModule(ActionBase): try: with open(source, 'r') as f: template_data = f.read() + + try: + template_uid = pwd.getpwuid(os.stat(source).st_uid).pw_name + except: + template_uid = os.stat(source).st_uid + + vars = task_vars.copy() + vars['template_host'] = os.uname()[1] + vars['template_path'] = source + vars['template_mtime'] = datetime.datetime.fromtimestamp(os.path.getmtime(source)) + vars['template_uid'] = template_uid + vars['template_fullpath'] = os.path.abspath(source) + vars['template_run_date'] = datetime.datetime.now() + + managed_default = C.DEFAULT_MANAGED_STR + managed_str = managed_default.format( + host = vars['template_host'], + uid = vars['template_uid'], + file = to_bytes(vars['template_path']) + ) + vars['ansible_managed'] = time.strftime( + managed_str, + time.localtime(os.path.getmtime(source)) + ) + + old_vars = self._templar._available_variables + self._templar.set_available_variables(vars) resultant = self._templar.template(template_data, preserve_trailing_newlines=True) + self._templar.set_available_variables(old_vars) except Exception as e: return dict(failed=True, msg=type(e).__name__ + ": " + str(e)) diff --git a/lib/ansible/vars/__init__.py b/lib/ansible/vars/__init__.py index 990f3660ee..740f8912fb 100644 --- a/lib/ansible/vars/__init__.py +++ b/lib/ansible/vars/__init__.py @@ -243,6 +243,8 @@ class VariableManager: # the 'omit' value alows params to be left out if the variable they are based on is undefined all_vars['omit'] = self._omit_token + + # make vars self referential, so people can do things like 'vars[var_name]' all_vars['vars'] = all_vars.copy() #CACHED_VARS[cache_entry] = all_vars