vars_prompt: add choices support for input validation - #85882
vars_prompt: add choices support for input validation#85882bhowmickkrishnendu wants to merge 5 commits into
Conversation
Add support for 'choices' key in vars_prompt to restrict user input to predefined options. When choices are specified: - Interactive prompts validate user input against allowed choices - Values from --extra-vars are also validated - Invalid values raise ValueError with clear error message This enhancement improves playbook usability by preventing invalid input and providing clear feedback to users.
2f44e52 to
8d772ea
Compare
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
FYI this sort of change would normally require tests. |
e7ff121 to
899d414
Compare
|
Thanks for the review! I’ve added integration tests under The tests cover:
Please let me know if you’d like additional scenarios included. |
|
This is long standing PR. Please verify! |
There was a problem hiding this comment.
Have you considered using a play argument spec instead? I'm not sure I see the benefit to adding 'choices' to 'vars_prompt' since the argument spec handles that and more.
For example:
# playbook.yml
- hosts: localhost
gather_facts: no
vars_prompt:
- name: from_vars_prompt
prompt: enter choice1 or choice2
validate_argspec: vars_prompt_options# playbook.meta.yml - name must match playbook
argument_specs:
vars_prompt_options:
options:
from_vars_prompt:
type: str
choices:
- choice1
- choice2
required: True| # 🔹 New: support for choices | ||
| choices = var.get("choices") |
There was a problem hiding this comment.
What if choices is an unsupported format or contains invalid choices?
| if self._tqm: | ||
| self._tqm.send_callback( | ||
| 'v2_playbook_on_vars_prompt', | ||
| vname, private, prompt, encrypt, confirm, | ||
| salt_size, salt, default, unsafe | ||
| ) |
There was a problem hiding this comment.
This seems like a change in behavior. Previously this occurred only iv the variable name is not in extra vars.
| # Skip prompt if provided via --extra-vars | ||
| if vname in self._variable_manager.extra_vars: | ||
| play.vars[vname] = self._variable_manager.extra_vars[vname] | ||
|
|
||
| # If choices are defined, validate extra-vars value too | ||
| if choices and play.vars[vname] not in choices: | ||
| raise ValueError( | ||
| f"Invalid value for '{vname}': " | ||
| f"'{play.vars[vname]}' not in allowed choices {choices}" | ||
| ) | ||
| continue |
There was a problem hiding this comment.
I don't think extra vars should be validated using choices.
|
|
||
| # 🔹 Check choices for interactive input | ||
| if choices: | ||
| while answer not in choices: |
There was a problem hiding this comment.
If there's no valid choice this will loop forever. I think some validation of the contents of choices should be added first.
|
@bhowmickkrishnendu stop spaming comments in other tickets, this is not a 'long standing PR' by any account, I could link hundreds that have been in the queue much longer. |
|
Thank you very much for your submission to Ansible. It means a lot to us that you've taken the time to contribute. Unfortunately, we currently ask that first-time contributors only have one open pull request. This will help you to learn what an acceptable pull request is like and also help us keep up with the review queue. Since you already have an active pull request open at #85368, we are automatically closing this one for now. Once your other PR is merged or closed, please feel free to reopen this one or submit it again. Thank you for understanding! |
Summary
This PR adds support for a new
choiceskey invars_prompt, allowing list-based input validation directly in playbooks. Users can now restrict input to predefined options, improving playbook usability and preventing invalid data entry.Feature
Example usage:
Behavior:
--extra-varsare also validated against the choices list.--extra-vars, aValueErroris raised with a clear error message.Example with --extra-vars:
Valid usage:
ansible-playbook playbook.yml --extra-vars "color=red"Invalid usage (raises error):
ansible-playbook playbook.yml --extra-vars "color=yellow"Error:
Changes Made
lib/ansible/playbook/play.py: Added'choices'to the list of allowed keys invars_promptvalidation.lib/ansible/executor/playbook_executor.py:--extra-vars.changelogs/fragments/vars_prompt_choices_support.yml: Added changelog fragment documenting the new feature.Notes
choicesis explicitly used.--extra-varsare validated.ISSUE TYPE
Testing
Test interactive prompts with valid/invalid choices:
Test --extra-vars validation with valid choice:
ansible-playbook test_choices.yml --extra-vars "color=red"Test --extra-vars validation with invalid choice (should raise error):
ansible-playbook test_choices.yml --extra-vars "color=yellow"