mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-05-08 06:12:51 +00:00
Introduce the 'always_run' task clause.
The 'always_run' task clause allows one to execute a task even in check mode. While here implement Runner.noop_on_check() to check if a runner really should execute its task, with respect to check mode option and 'always_run' clause. Also add the optional 'jinja2' argument to check_conditional() : it allows to give this function a jinja2 expression without exposing the 'jinja2_compare' implementation mechanism.
This commit is contained in:
@@ -37,6 +37,7 @@ import ansible.constants as C
|
||||
import ansible.inventory
|
||||
from ansible import utils
|
||||
from ansible.utils import template
|
||||
from ansible.utils import check_conditional
|
||||
from ansible import errors
|
||||
from ansible import module_common
|
||||
import poller
|
||||
@@ -156,6 +157,7 @@ class Runner(object):
|
||||
self.inventory = utils.default(inventory, lambda: ansible.inventory.Inventory(host_list))
|
||||
|
||||
self.module_vars = utils.default(module_vars, lambda: {})
|
||||
self.always_run = None
|
||||
self.connector = connection.Connection(self)
|
||||
self.conditional = conditional
|
||||
self.module_name = module_name
|
||||
@@ -935,3 +937,16 @@ class Runner(object):
|
||||
self.background = time_limit
|
||||
results = self.run()
|
||||
return results, poller.AsyncPoller(results, self)
|
||||
|
||||
# *****************************************************
|
||||
|
||||
def noop_on_check(self, inject):
|
||||
''' Should the runner run in check mode or not ? '''
|
||||
|
||||
# initialize self.always_run on first call
|
||||
if self.always_run is None:
|
||||
self.always_run = self.module_vars.get('always_run', False)
|
||||
self.always_run = check_conditional(
|
||||
self.always_run, self.basedir, inject, fail_on_undefined=True, jinja2=True)
|
||||
|
||||
return (self.check and not self.always_run)
|
||||
|
||||
@@ -36,7 +36,7 @@ class ActionModule(object):
|
||||
|
||||
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
|
||||
|
||||
if self.runner.check:
|
||||
if self.runner.noop_on_check(inject):
|
||||
return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not supported for this module'))
|
||||
|
||||
args = {}
|
||||
|
||||
@@ -25,7 +25,7 @@ class ActionModule(object):
|
||||
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
|
||||
''' transfer the given module name, plus the async module, then run it '''
|
||||
|
||||
if self.runner.check:
|
||||
if self.runner.noop_on_check(inject):
|
||||
return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not supported for this module'))
|
||||
|
||||
# shell and command module are the same
|
||||
|
||||
@@ -126,7 +126,7 @@ class ActionModule(object):
|
||||
else:
|
||||
diff = {}
|
||||
|
||||
if self.runner.check:
|
||||
if self.runner.noop_on_check(inject):
|
||||
if content is not None:
|
||||
os.remove(tmp_content)
|
||||
return ReturnData(conn=conn, result=dict(changed=True), diff=diff)
|
||||
@@ -172,7 +172,7 @@ class ActionModule(object):
|
||||
# don't send down raw=no
|
||||
module_args.pop('raw')
|
||||
module_args = "%s src=%s" % (module_args, pipes.quote(tmp_src))
|
||||
if self.runner.check:
|
||||
if self.runner.noop_on_check(inject):
|
||||
module_args = "%s CHECKMODE=True" % module_args
|
||||
return self.runner._execute_module(conn, tmp, 'file', module_args, inject=inject, complex_args=complex_args)
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ class ActionModule(object):
|
||||
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
|
||||
''' handler for fetch operations '''
|
||||
|
||||
if self.runner.check:
|
||||
if self.runner.noop_on_check(inject):
|
||||
return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not (yet) supported for this module'))
|
||||
|
||||
# load up options
|
||||
|
||||
@@ -38,7 +38,7 @@ class ActionModule(object):
|
||||
|
||||
module_args = self.runner._complex_args_hack(complex_args, module_args)
|
||||
|
||||
if self.runner.check:
|
||||
if self.runner.noop_on_check(inject):
|
||||
if module_name in [ 'shell', 'command' ]:
|
||||
return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not supported for %s' % module_name))
|
||||
# else let the module parsing code decide, though this will only be allowed for AnsibleModuleCommon using
|
||||
|
||||
@@ -30,7 +30,7 @@ class ActionModule(object):
|
||||
|
||||
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
|
||||
|
||||
if self.runner.check:
|
||||
if self.runner.noop_on_check(inject):
|
||||
# in --check mode, always skip this module execution
|
||||
return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True))
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ class ActionModule(object):
|
||||
def run(self, conn, tmp, module_name, module_args, inject, complex_args=None, **kwargs):
|
||||
''' handler for file transfer operations '''
|
||||
|
||||
if self.runner.check:
|
||||
if self.runner.noop_on_check(inject):
|
||||
# in check mode, always skip this module
|
||||
return ReturnData(conn=conn, comm_ok=True, result=dict(skipped=True, msg='check mode not supported for this module'))
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ class ActionModule(object):
|
||||
# run the copy module
|
||||
module_args = "%s src=%s dest=%s original_basename=%s" % (module_args, pipes.quote(xfered), pipes.quote(dest), pipes.quote(os.path.basename(source)))
|
||||
|
||||
if self.runner.check:
|
||||
if self.runner.noop_on_check(inject):
|
||||
return ReturnData(conn=conn, comm_ok=True, result=dict(changed=True), diff=dict(before_header=dest, after_header=source, before=dest_contents, after=resultant))
|
||||
else:
|
||||
res = self.runner._execute_module(conn, tmp, 'copy', module_args, inject=inject, complex_args=complex_args)
|
||||
|
||||
Reference in New Issue
Block a user