summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFelix Fontein <felix@fontein.de>2021-03-25 00:17:07 +0100
committerGitHub <noreply@github.com>2021-03-25 00:17:07 +0100
commit940b7af7d8242d40c59fc05a6ef4db3f3629552c (patch)
treea4879a939b049bc899a3bc6349170cbcdd9f3b58
parentUpdating Community docs with DO information (#74019) (diff)
downloadansible-940b7af7d8242d40c59fc05a6ef4db3f3629552c.tar.xz
ansible-940b7af7d8242d40c59fc05a6ef4db3f3629552c.zip
argspec validation - fix apply_defaults (#74029)
* Add test
-rw-r--r--changelogs/fragments/74029-argspec-apply_defaults.yml2
-rw-r--r--lib/ansible/module_utils/common/parameters.py4
-rw-r--r--test/integration/targets/argspec/library/argspec.py13
-rw-r--r--test/integration/targets/argspec/tasks/main.yml29
4 files changed, 46 insertions, 2 deletions
diff --git a/changelogs/fragments/74029-argspec-apply_defaults.yml b/changelogs/fragments/74029-argspec-apply_defaults.yml
new file mode 100644
index 0000000000..ec7c56e09c
--- /dev/null
+++ b/changelogs/fragments/74029-argspec-apply_defaults.yml
@@ -0,0 +1,2 @@
+bugfixes:
+- "argument spec validation - fix behavior of ``apply_defaults=True`` when an empty dictionary is specified for such an option (https://github.com/ansible/ansible/pull/74029)."
diff --git a/lib/ansible/module_utils/common/parameters.py b/lib/ansible/module_utils/common/parameters.py
index e297573410..972db2d3fa 100644
--- a/lib/ansible/module_utils/common/parameters.py
+++ b/lib/ansible/module_utils/common/parameters.py
@@ -702,8 +702,8 @@ def _validate_sub_spec(argument_spec, parameters, prefix='', options_context=Non
if sub_spec is not None:
if parameters.get(param) is None:
parameters[param] = {}
- else:
- continue
+ else:
+ continue
elif sub_spec is None or param not in parameters or parameters[param] is None:
continue
diff --git a/test/integration/targets/argspec/library/argspec.py b/test/integration/targets/argspec/library/argspec.py
index 08dad1a049..1a1d288d23 100644
--- a/test/integration/targets/argspec/library/argspec.py
+++ b/test/integration/targets/argspec/library/argspec.py
@@ -126,6 +126,19 @@ def main():
'int': {
'type': 'int',
},
+ 'apply_defaults': {
+ 'type': 'dict',
+ 'apply_defaults': True,
+ 'options': {
+ 'foo': {
+ 'type': 'str',
+ },
+ 'bar': {
+ 'type': 'str',
+ 'default': 'baz',
+ },
+ },
+ },
},
required_if=(
('state', 'present', ('path', 'content'), True),
diff --git a/test/integration/targets/argspec/tasks/main.yml b/test/integration/targets/argspec/tasks/main.yml
index d90bdf0245..283c922d78 100644
--- a/test/integration/targets/argspec/tasks/main.yml
+++ b/test/integration/targets/argspec/tasks/main.yml
@@ -342,6 +342,30 @@
register: argspec_int_invalid
ignore_errors: true
+- argspec:
+ required: value
+ required_one_of_one: value
+ register: argspec_apply_defaults_not_specified
+
+- argspec:
+ required: value
+ required_one_of_one: value
+ apply_defaults: ~
+ register: argspec_apply_defaults_none
+
+- argspec:
+ required: value
+ required_one_of_one: value
+ apply_defaults: {}
+ register: argspec_apply_defaults_empty
+
+- argspec:
+ required: value
+ required_one_of_one: value
+ apply_defaults:
+ foo: bar
+ register: argspec_apply_defaults_one
+
- assert:
that:
- argspec_required_fail is failed
@@ -417,3 +441,8 @@
- argspec_password_no_log.stdout|regex_findall('VALUE_SPECIFIED_IN_NO_LOG_PARAMETER')|length == 1
- argspec_int_invalid is failed
+
+ - "argspec_apply_defaults_not_specified.apply_defaults == {'foo': none, 'bar': 'baz'}"
+ - "argspec_apply_defaults_none.apply_defaults == {'foo': none, 'bar': 'baz'}"
+ - "argspec_apply_defaults_empty.apply_defaults == {'foo': none, 'bar': 'baz'}"
+ - "argspec_apply_defaults_one.apply_defaults == {'foo': 'bar', 'bar': 'baz'}"