mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
* cmd_runner: added flag skip_if_check_mode to context
* added changelog fragment
* adjusted param name and added new one
(cherry picked from commit be69f95f63
)
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
parent
aea851018b
commit
9f0913bf73
5 changed files with 59 additions and 35 deletions
2
changelogs/fragments/4736-cmd-runner-skip-if-check.yml
Normal file
2
changelogs/fragments/4736-cmd-runner-skip-if-check.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
minor_changes:
|
||||||
|
- 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).
|
|
@ -197,7 +197,7 @@ class CmdRunner(object):
|
||||||
if mod_param_name not in self.arg_formats:
|
if mod_param_name not in self.arg_formats:
|
||||||
self.arg_formats[mod_param_name] = _Format.as_default_type(spec['type'], mod_param_name)
|
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, **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:
|
if output_process is None:
|
||||||
output_process = _process_as_is
|
output_process = _process_as_is
|
||||||
if args_order is None:
|
if args_order is None:
|
||||||
|
@ -209,18 +209,22 @@ class CmdRunner(object):
|
||||||
return _CmdRunnerContext(runner=self,
|
return _CmdRunnerContext(runner=self,
|
||||||
args_order=args_order,
|
args_order=args_order,
|
||||||
output_process=output_process,
|
output_process=output_process,
|
||||||
ignore_value_none=ignore_value_none, **kwargs)
|
ignore_value_none=ignore_value_none,
|
||||||
|
check_mode_skip=check_mode_skip,
|
||||||
|
check_mode_return=check_mode_return, **kwargs)
|
||||||
|
|
||||||
def has_arg_format(self, arg):
|
def has_arg_format(self, arg):
|
||||||
return arg in self.arg_formats
|
return arg in self.arg_formats
|
||||||
|
|
||||||
|
|
||||||
class _CmdRunnerContext(object):
|
class _CmdRunnerContext(object):
|
||||||
def __init__(self, runner, args_order, output_process, ignore_value_none, **kwargs):
|
def __init__(self, runner, args_order, output_process, ignore_value_none, check_mode_skip, check_mode_return, **kwargs):
|
||||||
self.runner = runner
|
self.runner = runner
|
||||||
self.args_order = tuple(args_order)
|
self.args_order = tuple(args_order)
|
||||||
self.output_process = output_process
|
self.output_process = output_process
|
||||||
self.ignore_value_none = ignore_value_none
|
self.ignore_value_none = ignore_value_none
|
||||||
|
self.check_mode_skip = check_mode_skip
|
||||||
|
self.check_mode_return = check_mode_return
|
||||||
self.run_command_args = dict(kwargs)
|
self.run_command_args = dict(kwargs)
|
||||||
|
|
||||||
self.environ_update = runner.environ_update
|
self.environ_update = runner.environ_update
|
||||||
|
@ -260,6 +264,8 @@ class _CmdRunnerContext(object):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise FormatError(arg_name, value, runner.arg_formats[arg_name], e)
|
raise FormatError(arg_name, value, runner.arg_formats[arg_name], e)
|
||||||
|
|
||||||
|
if self.check_mode_skip and module.check_mode:
|
||||||
|
return self.check_mode_return
|
||||||
results = module.run_command(self.cmd, **self.run_command_args)
|
results = module.run_command(self.cmd, **self.run_command_args)
|
||||||
self.results_rc, self.results_out, self.results_err = results
|
self.results_rc, self.results_out, self.results_err = results
|
||||||
self.results_processed = self.output_process(*results)
|
self.results_processed = self.output_process(*results)
|
||||||
|
|
|
@ -6,36 +6,8 @@
|
||||||
from __future__ import absolute_import, division, print_function
|
from __future__ import absolute_import, division, print_function
|
||||||
__metaclass__ = type
|
__metaclass__ = type
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
DOCUMENTATION = '''
|
DOCUMENTATION = ""
|
||||||
module: cmd_echo
|
|
||||||
author: "Alexei Znamensky (@russoz)"
|
|
||||||
short_description: Simple module for testing
|
|
||||||
description:
|
|
||||||
- Simple module test description.
|
|
||||||
options:
|
|
||||||
command:
|
|
||||||
description: aaa
|
|
||||||
type: list
|
|
||||||
elements: str
|
|
||||||
required: true
|
|
||||||
arg_formats:
|
|
||||||
description: bbb
|
|
||||||
type: dict
|
|
||||||
required: true
|
|
||||||
arg_order:
|
|
||||||
description: ccc
|
|
||||||
type: raw
|
|
||||||
required: true
|
|
||||||
arg_values:
|
|
||||||
description: ddd
|
|
||||||
type: list
|
|
||||||
required: true
|
|
||||||
aa:
|
|
||||||
description: eee
|
|
||||||
type: raw
|
|
||||||
'''
|
|
||||||
|
|
||||||
EXAMPLES = ""
|
EXAMPLES = ""
|
||||||
|
|
||||||
|
@ -51,11 +23,15 @@ def main():
|
||||||
arg_formats=dict(type="dict", default={}),
|
arg_formats=dict(type="dict", default={}),
|
||||||
arg_order=dict(type="raw", required=True),
|
arg_order=dict(type="raw", required=True),
|
||||||
arg_values=dict(type="dict", default={}),
|
arg_values=dict(type="dict", default={}),
|
||||||
|
check_mode_skip=dict(type="bool", default=False),
|
||||||
aa=dict(type="raw"),
|
aa=dict(type="raw"),
|
||||||
),
|
),
|
||||||
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
p = module.params
|
p = module.params
|
||||||
|
|
||||||
|
info = None
|
||||||
|
|
||||||
arg_formats = {}
|
arg_formats = {}
|
||||||
for arg, fmt_spec in p['arg_formats'].items():
|
for arg, fmt_spec in p['arg_formats'].items():
|
||||||
func = getattr(fmt, fmt_spec['func'])
|
func = getattr(fmt, fmt_spec['func'])
|
||||||
|
@ -65,11 +41,11 @@ def main():
|
||||||
|
|
||||||
runner = CmdRunner(module, ['echo', '--'], arg_formats=arg_formats)
|
runner = CmdRunner(module, ['echo', '--'], arg_formats=arg_formats)
|
||||||
|
|
||||||
info = None
|
with runner.context(p['arg_order'], check_mode_skip=p['check_mode_skip']) as ctx:
|
||||||
with runner.context(p['arg_order']) as ctx:
|
|
||||||
result = ctx.run(**p['arg_values'])
|
result = ctx.run(**p['arg_values'])
|
||||||
info = ctx.run_info
|
info = ctx.run_info
|
||||||
rc, out, err = result
|
check = "check"
|
||||||
|
rc, out, err = result if result is not None else (None, None, None)
|
||||||
|
|
||||||
module.exit_json(rc=rc, out=out, err=err, info=info)
|
module.exit_json(rc=rc, out=out, err=err, info=info)
|
||||||
|
|
||||||
|
|
|
@ -4,8 +4,10 @@
|
||||||
arg_formats: "{{ item.arg_formats|default(omit) }}"
|
arg_formats: "{{ item.arg_formats|default(omit) }}"
|
||||||
arg_order: "{{ item.arg_order }}"
|
arg_order: "{{ item.arg_order }}"
|
||||||
arg_values: "{{ item.arg_values|default(omit) }}"
|
arg_values: "{{ item.arg_values|default(omit) }}"
|
||||||
|
check_mode_skip: "{{ item.check_mode_skip|default(omit) }}"
|
||||||
aa: "{{ item.aa|default(omit) }}"
|
aa: "{{ item.aa|default(omit) }}"
|
||||||
register: test_result
|
register: test_result
|
||||||
|
check_mode: "{{ item.check_mode|default(omit) }}"
|
||||||
ignore_errors: "{{ item.expect_error|default(omit) }}"
|
ignore_errors: "{{ item.expect_error|default(omit) }}"
|
||||||
|
|
||||||
- name: check results [{{ item.name }}]
|
- name: check results [{{ item.name }}]
|
||||||
|
|
|
@ -82,3 +82,41 @@ cmd_echo_tests:
|
||||||
- >-
|
- >-
|
||||||
"MissingArgumentValue: Cannot find value for parameter bb"
|
"MissingArgumentValue: Cannot find value for parameter bb"
|
||||||
in test_result.module_stderr
|
in test_result.module_stderr
|
||||||
|
|
||||||
|
- name: set aa and bb value with check_mode on
|
||||||
|
arg_formats:
|
||||||
|
aa:
|
||||||
|
func: as_opt_eq_val
|
||||||
|
args: [--answer]
|
||||||
|
bb:
|
||||||
|
func: as_bool
|
||||||
|
args: [--bb-here]
|
||||||
|
arg_order: 'aa bb'
|
||||||
|
arg_values:
|
||||||
|
bb: true
|
||||||
|
aa: 11
|
||||||
|
check_mode: true
|
||||||
|
assertions:
|
||||||
|
- test_result.rc == 0
|
||||||
|
- test_result.out == "-- --answer=11 --bb-here\n"
|
||||||
|
- test_result.err == ""
|
||||||
|
|
||||||
|
- name: set aa and bb value with check_mode and check_mode_skip on
|
||||||
|
arg_formats:
|
||||||
|
aa:
|
||||||
|
func: as_opt_eq_val
|
||||||
|
args: [--answer]
|
||||||
|
bb:
|
||||||
|
func: as_bool
|
||||||
|
args: [--bb-here]
|
||||||
|
arg_order: 'aa bb'
|
||||||
|
arg_values:
|
||||||
|
bb: true
|
||||||
|
check_mode_skip: true
|
||||||
|
aa: 11
|
||||||
|
check_mode: true
|
||||||
|
expect_error: true # because if result contains rc != 0, ansible assumes error
|
||||||
|
assertions:
|
||||||
|
- test_result.rc == None
|
||||||
|
- test_result.out == None
|
||||||
|
- test_result.err == None
|
||||||
|
|
Loading…
Reference in a new issue