Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate_args_spec, use combine vars and in precedence #75471

Merged
merged 8 commits into from Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/vas_fixes.yml
@@ -0,0 +1,2 @@
bugfixes:
- validate_argument_spec, correct variable precedence and merge method and add missing examples
33 changes: 33 additions & 0 deletions lib/ansible/modules/validate_argument_spec.py
Expand Up @@ -28,6 +28,39 @@
'''

EXAMPLES = r'''
- name: verify vars needed for this task file are present when included
validate_argument_spec:
argument_spec: '{{required_data}}'
vars:
required_data:
# unlike spec file, just put the options in directly
stuff:
description: stuff
type: str
choices: ['who', 'knows', 'what']
default: what
but:
description: i guess we need one
type: str
required: true


- name: verify vars needed for this task file are present when included, with spec from a spec file
validate_argument_spec:
argument_spec: "{{lookup('file', 'myargspec.yml')['specname']['options']}}"


- name: verify vars needed for next include and not from inside it, also with params i'll only define there
block:
- validate_argument_spec:
argument_spec: "{{lookup('file', 'nakedoptions.yml'}}"
provided_arguments:
but: "that i can define on the include itself, like in it's `vars:` keyword"

- name: the include itself
vars:
stuff: knows
but: nobuts!
'''

RETURN = r'''
Expand Down
5 changes: 2 additions & 3 deletions lib/ansible/plugins/action/validate_argument_spec.py
Expand Up @@ -9,6 +9,7 @@
from ansible.module_utils.six import iteritems, string_types
from ansible.module_utils.common.arg_spec import ArgumentSpecValidator
from ansible.module_utils.errors import AnsibleValidationErrorMultiple
from ansible.utils.vars import combine_vars


class ActionModule(ActionBase):
Expand Down Expand Up @@ -77,10 +78,8 @@ def run(self, tmp=None, task_vars=None):
raise AnsibleError('Incorrect type for provided_arguments, expected dict and got %s' % type(provided_arguments))

args_from_vars = self.get_args_from_task_vars(argument_spec_data, task_vars)
provided_arguments.update(args_from_vars)

validator = ArgumentSpecValidator(argument_spec_data)
validation_result = validator.validate(provided_arguments)
validation_result = validator.validate(combine_vars(args_from_vars, provided_arguments))

if validation_result.error_messages:
result['failed'] = True
Expand Down