mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 22:02:50 +00:00
started implementing diff
diff now works with template also fixed check mode for template and copy
This commit is contained in:
@@ -233,8 +233,7 @@ class ActionBase:
|
||||
Takes a remote checksum and returns 1 if no file
|
||||
'''
|
||||
|
||||
# FIXME: figure out how this will work, probably pulled from the
|
||||
# variable manager data
|
||||
# FIXME: figure out how this will work, probably pulled from the variable manager data
|
||||
#python_interp = inject['hostvars'][inject['inventory_hostname']].get('ansible_python_interpreter', 'python')
|
||||
python_interp = 'python'
|
||||
cmd = self._connection._shell.checksum(path, python_interp)
|
||||
|
||||
@@ -174,17 +174,11 @@ class ActionModule(ActionBase):
|
||||
if tmp is None or "-tmp-" not in tmp:
|
||||
tmp = self._make_tmp_path()
|
||||
|
||||
# FIXME: runner shouldn't have the diff option there
|
||||
#if self.runner.diff and not raw:
|
||||
# diff = self._get_diff_data(tmp, dest_file, source_full, task_vars)
|
||||
#else:
|
||||
# diff = {}
|
||||
diff = {}
|
||||
if self._play_context.diff and not raw:
|
||||
diffs.append(self._get_diff_data(tmp, dest_file, source_full, task_vars))
|
||||
|
||||
if self._play_context.check_mode:
|
||||
self._remove_tempfile_if_content_defined(content, content_tempfile)
|
||||
# FIXME: diff stuff
|
||||
#diffs.append(diff)
|
||||
changed = True
|
||||
module_return = dict(changed=True)
|
||||
continue
|
||||
@@ -231,7 +225,7 @@ class ActionModule(ActionBase):
|
||||
|
||||
if raw:
|
||||
# Continue to next iteration if raw is defined.
|
||||
# self._remove_tmp_path(tmp)
|
||||
self._remove_tmp_path(tmp)
|
||||
continue
|
||||
|
||||
# Build temporary module_args.
|
||||
@@ -244,6 +238,9 @@ class ActionModule(ActionBase):
|
||||
)
|
||||
)
|
||||
|
||||
if self._play_context.check_mode:
|
||||
new_module_args['CHECKMODE'] = True
|
||||
|
||||
# Execute the file module.
|
||||
module_return = self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars, delete_remote_tmp=delete_remote_tmp)
|
||||
module_executed = True
|
||||
@@ -299,9 +296,8 @@ class ActionModule(ActionBase):
|
||||
diff['before'] = ''
|
||||
elif peek_result['appears_binary']:
|
||||
diff['dst_binary'] = 1
|
||||
# FIXME: this should not be in utils..
|
||||
#elif peek_result['size'] > utils.MAX_FILE_SIZE_FOR_DIFF:
|
||||
# diff['dst_larger'] = utils.MAX_FILE_SIZE_FOR_DIFF
|
||||
elif peek_result['size'] > C.MAX_FILE_SIZE_FOR_DIFF:
|
||||
diff['dst_larger'] = C.MAX_FILE_SIZE_FOR_DIFF
|
||||
else:
|
||||
dest_result = self._execute_module(module_name='slurp', module_args=dict(path=destination), task_vars=task_vars, tmp=tmp, persist_files=True)
|
||||
if 'content' in dest_result:
|
||||
@@ -318,9 +314,8 @@ class ActionModule(ActionBase):
|
||||
st = os.stat(source)
|
||||
if "\x00" in src_contents:
|
||||
diff['src_binary'] = 1
|
||||
# FIXME: this should not be in utils
|
||||
#elif st[stat.ST_SIZE] > utils.MAX_FILE_SIZE_FOR_DIFF:
|
||||
# diff['src_larger'] = utils.MAX_FILE_SIZE_FOR_DIFF
|
||||
elif st[stat.ST_SIZE] > C.MAX_FILE_SIZE_FOR_DIFF:
|
||||
diff['src_larger'] = C.MAX_FILE_SIZE_FOR_DIFF
|
||||
else:
|
||||
src.seek(0)
|
||||
diff['after_header'] = source
|
||||
|
||||
@@ -125,40 +125,44 @@ class ActionModule(ActionBase):
|
||||
return remote_checksum
|
||||
|
||||
if local_checksum != remote_checksum:
|
||||
# if showing diffs, we need to get the remote value
|
||||
dest_contents = ''
|
||||
|
||||
# FIXME: still need to implement diff mechanism
|
||||
#if self.runner.diff:
|
||||
# # using persist_files to keep the temp directory around to avoid needing to grab another
|
||||
# dest_result = self.runner._execute_module(conn, tmp, 'slurp', "path=%s" % dest, task_vars=task_vars, persist_files=True)
|
||||
# if 'content' in dest_result.result:
|
||||
# dest_contents = dest_result.result['content']
|
||||
# if dest_result.result['encoding'] == 'base64':
|
||||
# dest_contents = base64.b64decode(dest_contents)
|
||||
# else:
|
||||
# raise Exception("unknown encoding, failed: %s" % dest_result.result)
|
||||
# if showing diffs, we need to get the remote value
|
||||
if self._play_context.diff:
|
||||
# using persist_files to keep the temp directory around to avoid needing to grab another
|
||||
my_args = dict(path=dest)
|
||||
dest_result = self._execute_module(module_name='slurp', module_args=my_args, task_vars=task_vars, persist_files=True)
|
||||
if 'content' in dest_result:
|
||||
dest_contents = dest_result['content']
|
||||
if dest_result['encoding'] == 'base64':
|
||||
dest_contents = base64.b64decode(dest_contents)
|
||||
else:
|
||||
raise Exception("unknown encoding, failed: %s" % dest_result)
|
||||
|
||||
xfered = self._transfer_data(self._connection._shell.join_path(tmp, 'source'), resultant)
|
||||
if not self._play_context.check_mode: # do actual work thorugh copy
|
||||
xfered = self._transfer_data(self._connection._shell.join_path(tmp, 'source'), resultant)
|
||||
|
||||
# fix file permissions when the copy is done as a different user
|
||||
if self._play_context.become and self._play_context.become_user != 'root':
|
||||
self._remote_chmod('a+r', xfered, tmp)
|
||||
# fix file permissions when the copy is done as a different user
|
||||
if self._play_context.become and self._play_context.become_user != 'root':
|
||||
self._remote_chmod('a+r', xfered, tmp)
|
||||
|
||||
# run the copy module
|
||||
new_module_args = self._task.args.copy()
|
||||
new_module_args.update(
|
||||
dict(
|
||||
src=xfered,
|
||||
dest=dest,
|
||||
original_basename=os.path.basename(source),
|
||||
follow=True,
|
||||
),
|
||||
)
|
||||
# run the copy module
|
||||
new_module_args = self._task.args.copy()
|
||||
new_module_args.update(
|
||||
dict(
|
||||
src=xfered,
|
||||
dest=dest,
|
||||
original_basename=os.path.basename(source),
|
||||
follow=True,
|
||||
),
|
||||
)
|
||||
result = self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars)
|
||||
else:
|
||||
result=dict(changed=True)
|
||||
|
||||
if result.['changed'] and self._play_context.diff:
|
||||
result['diff'] = dict(before=dest_contents, after=resultant, before_header=dest, after_header=source)
|
||||
|
||||
result = self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars)
|
||||
if result.get('changed', False):
|
||||
result['diff'] = dict(before=dest_contents, after=resultant)
|
||||
return result
|
||||
|
||||
else:
|
||||
@@ -177,5 +181,8 @@ class ActionModule(ActionBase):
|
||||
),
|
||||
)
|
||||
|
||||
if self._play_context.check_mode:
|
||||
new_module_args['CHECKMODE'] = True
|
||||
|
||||
return self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user