Add fallback option when stating the source file

This change will now fall back to using the ansible core module
`stat` to stat a file when the local stat function fails for a
permissions error. This is being done because there is an issue
with action plugins not respecting "become: true" on local files
being written over a local connection.

Change-Id: I646f0688f1f4bf3cddaef9114dac51c2a807b25b
Signed-off-by: Kevin Carter <kecarter@redhat.com>
This commit is contained in:
Kevin Carter 2019-12-05 12:40:43 -06:00
parent ff61269489
commit 420acdf0e4
No known key found for this signature in database
GPG Key ID: CE94BD890A47B20A
1 changed files with 46 additions and 11 deletions

View File

@ -58,6 +58,12 @@ CONFIG_TYPES = {
STRIP_MARKER = '__MARKER__'
# Py3 vs Py2 error handling. When Py2 is no longer supported, remove this.
try:
PermissionError = PermissionError
except NameError:
PermissionError = (IOError, OSError)
class IDumper(AnsibleDumper):
def increase_indent(self, flow=False, indentless=False):
@ -778,16 +784,33 @@ class ActionModule(ActionBase):
temp_vars = task_vars.copy()
template_host = temp_vars['template_host'] = os.uname()[1]
source = temp_vars['template_path'] = _vars['source']
temp_vars['template_mtime'] = datetime.datetime.fromtimestamp(
os.path.getmtime(source)
)
try:
template_uid = temp_vars['template_uid'] = pwd.getpwuid(
os.stat(source).st_uid
).pw_name
except Exception:
template_uid = temp_vars['template_uid'] = os.stat(source).st_uid
mtime = os.path.getmtime(source)
temp_vars['template_mtime'] = datetime.datetime.fromtimestamp(
mtime
)
try:
template_uid = temp_vars['template_uid'] = pwd.getpwuid(
os.stat(source).st_uid
).pw_name
except Exception:
template_uid = temp_vars['template_uid'] = os.stat(
source
).st_uid
except PermissionError:
local_task_vars = temp_vars.copy()
local_task_vars['connection'] = 'local'
stat = self._execute_module(
module_name='stat',
module_args=dict(path=source),
task_vars=local_task_vars
)
mtime = stat['stat']['mtime']
temp_vars['template_mtime'] = datetime.datetime.fromtimestamp(
mtime
)
template_uid = stat['stat']['uid']
managed_default = C.DEFAULT_MANAGED_STR
managed_str = managed_default.format(
@ -798,13 +821,25 @@ class ActionModule(ActionBase):
temp_vars['ansible_managed'] = time.strftime(
managed_str,
time.localtime(os.path.getmtime(source))
time.localtime(mtime)
)
temp_vars['template_fullpath'] = os.path.abspath(source)
temp_vars['template_run_date'] = datetime.datetime.now()
with open(source, 'r') as f:
template_data = to_text(f.read())
try:
with open(source, 'r') as f:
template_data = to_text(f.read())
except PermissionError:
local_temp_vars = task_vars.copy()
local_temp_vars['connection'] = 'local'
template_data_slurpee = self._execute_module(
module_name='slurp',
module_args=dict(src=_vars['dest']),
task_vars=local_temp_vars
)
template_data = base64.b64decode(
template_data_slurpee['content']
).decode('utf-8')
self._templar.environment.loader.searchpath = _vars['searchpath']
self._templar.set_available_variables(temp_vars)