Skip to content

Commit

Permalink
adjusted param name and added new one
Browse files Browse the repository at this point in the history
  • Loading branch information
russoz committed May 28, 2022
1 parent 405eb89 commit 9f55ab6
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 12 deletions.
2 changes: 1 addition & 1 deletion changelogs/fragments/4736-cmd-runner-skip-if-check.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
minor_changes:
- cmd_runner module util - added parameter ``skip_if_check_mode`` to ``CmdRunner.context()``, so that the command is not executed when ``check_mode=True`` (https://github.com/ansible-collections/community.general/pull/4736).
- cmd_runner module util - added parameters ``check_mode_skip`` and ``check_mode_return`` to ``CmdRunner.context()``, so that the command is not executed when ``check_mode=True`` (https://github.com/ansible-collections/community.general/pull/4736).
14 changes: 8 additions & 6 deletions plugins/module_utils/cmd_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def __init__(self, module, command, arg_formats=None, default_args_order=(),
if mod_param_name not in self.arg_formats:
self.arg_formats[mod_param_name] = _Format.as_default_type(spec['type'], mod_param_name)

def context(self, args_order=None, output_process=None, ignore_value_none=True, skip_if_check_mode=False, **kwargs):
def context(self, args_order=None, output_process=None, ignore_value_none=True, check_mode_skip=False, check_mode_return=None, **kwargs):
if output_process is None:
output_process = _process_as_is
if args_order is None:
Expand All @@ -210,19 +210,21 @@ def context(self, args_order=None, output_process=None, ignore_value_none=True,
args_order=args_order,
output_process=output_process,
ignore_value_none=ignore_value_none,
skip_if_check_mode=skip_if_check_mode, **kwargs)
check_mode_skip=check_mode_skip,
check_mode_return=check_mode_return, **kwargs)

def has_arg_format(self, arg):
return arg in self.arg_formats


class _CmdRunnerContext(object):
def __init__(self, runner, args_order, output_process, ignore_value_none, skip_if_check_mode, **kwargs):
def __init__(self, runner, args_order, output_process, ignore_value_none, check_mode_skip, check_mode_return, **kwargs):
self.runner = runner
self.args_order = tuple(args_order)
self.output_process = output_process
self.ignore_value_none = ignore_value_none
self.skip_if_check_mode = skip_if_check_mode
self.check_mode_skip = check_mode_skip
self.check_mode_return = check_mode_return
self.run_command_args = dict(kwargs)

self.environ_update = runner.environ_update
Expand Down Expand Up @@ -262,8 +264,8 @@ def run(self, **kwargs):
except Exception as e:
raise FormatError(arg_name, value, runner.arg_formats[arg_name], e)

if self.skip_if_check_mode and module.check_mode:
return
if self.check_mode_skip and module.check_mode:
return self.check_mode_return
results = module.run_command(self.cmd, **self.run_command_args)
self.results_rc, self.results_out, self.results_err = results
self.results_processed = self.output_process(*results)
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/targets/cmd_runner/library/cmd_echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def main():
arg_formats=dict(type="dict", default={}),
arg_order=dict(type="raw", required=True),
arg_values=dict(type="dict", default={}),
skip_if_check=dict(type="bool", default=False),
check_mode_skip=dict(type="bool", default=False),
aa=dict(type="raw"),
),
supports_check_mode=True,
Expand All @@ -41,7 +41,7 @@ def main():

runner = CmdRunner(module, ['echo', '--'], arg_formats=arg_formats)

with runner.context(p['arg_order'], skip_if_check_mode=p['skip_if_check']) as ctx:
with runner.context(p['arg_order'], check_mode_skip=p['check_mode_skip']) as ctx:
result = ctx.run(**p['arg_values'])
info = ctx.run_info
check = "check"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
arg_formats: "{{ item.arg_formats|default(omit) }}"
arg_order: "{{ item.arg_order }}"
arg_values: "{{ item.arg_values|default(omit) }}"
skip_if_check: "{{ item.skip_if_check|default(omit) }}"
check_mode_skip: "{{ item.check_mode_skip|default(omit) }}"
aa: "{{ item.aa|default(omit) }}"
register: test_result
check_mode: "{{ item.check_mode|default(omit) }}"
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/targets/cmd_runner/vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ cmd_echo_tests:
- test_result.out == "-- --answer=11 --bb-here\n"
- test_result.err == ""

- name: set aa and bb value with check_mode and skip_if_check on
- name: set aa and bb value with check_mode and check_mode_skip on
arg_formats:
aa:
func: as_opt_eq_val
Expand All @@ -112,7 +112,7 @@ cmd_echo_tests:
arg_order: 'aa bb'
arg_values:
bb: true
skip_if_check: true
check_mode_skip: true
aa: 11
check_mode: true
expect_error: true # because if result contains rc != 0, ansible assumes error
Expand Down

0 comments on commit 9f55ab6

Please sign in to comment.