mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-07 13:52:54 +00:00
Very basic --diff option for showing what happens when templates change.
Probably output is not useful if not used with --limit Works well with --check mode
This commit is contained in:
@@ -118,11 +118,13 @@ class Runner(object):
|
||||
is_playbook=False, # running from playbook or not?
|
||||
inventory=None, # reference to Inventory object
|
||||
subset=None, # subset pattern
|
||||
check=False # don't make any changes, just try to probe for potential changes
|
||||
check=False, # don't make any changes, just try to probe for potential changes
|
||||
diff=False
|
||||
):
|
||||
|
||||
# storage & defaults
|
||||
self.check = check
|
||||
self.diff = diff
|
||||
self.setup_cache = utils.default(setup_cache, lambda: collections.defaultdict(dict))
|
||||
self.basedir = utils.default(basedir, lambda: os.getcwd())
|
||||
self.callbacks = utils.default(callbacks, lambda: DefaultRunnerCallbacks())
|
||||
@@ -192,7 +194,7 @@ class Runner(object):
|
||||
# *****************************************************
|
||||
|
||||
def _execute_module(self, conn, tmp, module_name, args,
|
||||
async_jid=None, async_module=None, async_limit=None, inject=None):
|
||||
async_jid=None, async_module=None, async_limit=None, inject=None, persist_files=False):
|
||||
|
||||
''' runs a module that has already been transferred '''
|
||||
|
||||
@@ -233,7 +235,7 @@ class Runner(object):
|
||||
raise errors.AnsibleError("module is missing interpreter line")
|
||||
|
||||
cmd = shebang.replace("#!","") + " " + cmd
|
||||
if tmp.find("tmp") != -1 and C.DEFAULT_KEEP_REMOTE_FILES != '1':
|
||||
if tmp.find("tmp") != -1 and C.DEFAULT_KEEP_REMOTE_FILES != '1' and not persist_files:
|
||||
cmd = cmd + "; rm -rf %s >/dev/null 2>&1" % tmp
|
||||
res = self._low_level_exec_command(conn, cmd, tmp, sudoable=True)
|
||||
data = utils.parse_json(res['stdout'])
|
||||
|
||||
@@ -20,6 +20,7 @@ import os
|
||||
from ansible import utils
|
||||
from ansible import errors
|
||||
from ansible.runner.return_data import ReturnData
|
||||
import base64
|
||||
|
||||
class ActionModule(object):
|
||||
|
||||
@@ -46,6 +47,7 @@ class ActionModule(object):
|
||||
|
||||
# if we have first_available_file in our vars
|
||||
# look up the files and use the first one we find as src
|
||||
|
||||
if 'first_available_file' in inject:
|
||||
found = False
|
||||
for fn in self.runner.module_vars.get('first_available_file'):
|
||||
@@ -79,18 +81,35 @@ class ActionModule(object):
|
||||
|
||||
# template is different from the remote value
|
||||
|
||||
xfered = self.runner._transfer_str(conn, tmp, 'source', resultant)
|
||||
# if showing diffs, we need to get the remote value
|
||||
dest_contents = None
|
||||
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, inject=inject, persist_files=True)
|
||||
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)
|
||||
|
||||
xfered = self.runner._transfer_str(conn, tmp, source, resultant)
|
||||
|
||||
# fix file permissions when the copy is done as a different user
|
||||
if self.runner.sudo and self.runner.sudo_user != 'root':
|
||||
self.runner._low_level_exec_command(conn, "chmod a+r %s" % xfered, tmp)
|
||||
|
||||
# run the copy module
|
||||
module_args = "%s src=%s dest=%s" % (module_args, xfered, dest)
|
||||
|
||||
|
||||
if self.runner.check:
|
||||
if self.runner.diff:
|
||||
self.runner.callbacks.on_file_diff(conn.host, dest_contents, resultant)
|
||||
return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True))
|
||||
else:
|
||||
return self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject)
|
||||
res = self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject)
|
||||
if self.runner.diff:
|
||||
self.runner.callbacks.on_file_diff(conn.host, dest_contents, resultant)
|
||||
return res
|
||||
else:
|
||||
return ReturnData(conn=conn, comm_ok=True, result=dict(changed=False))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user