parent
bfbb88b4a9
commit
f67949e42c
2 changed files with 34 additions and 0 deletions
|
@ -18,10 +18,14 @@ from __future__ import (absolute_import, division, print_function)
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
|
import datetime
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
from ansible import constants as C
|
||||||
from ansible.plugins.action import ActionBase
|
from ansible.plugins.action import ActionBase
|
||||||
from ansible.utils.hashing import checksum_s
|
from ansible.utils.hashing import checksum_s
|
||||||
|
from ansible.utils.unicode import to_bytes
|
||||||
|
|
||||||
class ActionModule(ActionBase):
|
class ActionModule(ActionBase):
|
||||||
|
|
||||||
|
@ -97,7 +101,35 @@ class ActionModule(ActionBase):
|
||||||
try:
|
try:
|
||||||
with open(source, 'r') as f:
|
with open(source, 'r') as f:
|
||||||
template_data = f.read()
|
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)
|
resultant = self._templar.template(template_data, preserve_trailing_newlines=True)
|
||||||
|
self._templar.set_available_variables(old_vars)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return dict(failed=True, msg=type(e).__name__ + ": " + str(e))
|
return dict(failed=True, msg=type(e).__name__ + ": " + str(e))
|
||||||
|
|
||||||
|
|
|
@ -243,6 +243,8 @@ class VariableManager:
|
||||||
|
|
||||||
# the 'omit' value alows params to be left out if the variable they are based on is undefined
|
# the 'omit' value alows params to be left out if the variable they are based on is undefined
|
||||||
all_vars['omit'] = self._omit_token
|
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()
|
all_vars['vars'] = all_vars.copy()
|
||||||
|
|
||||||
#CACHED_VARS[cache_entry] = all_vars
|
#CACHED_VARS[cache_entry] = all_vars
|
||||||
|
|
Loading…
Reference in a new issue