Skip to content

Add 'validate' regex support for vars_prompt - #85368

Closed
bhowmickkrishnendu wants to merge 4 commits into
ansible:develfrom
bhowmickkrishnendu:feature-vars-prompt-validate
Closed

bhowmickkrishnendu wants to merge 4 commits into
ansible:develfrom
bhowmickkrishnendu:feature-vars-prompt-validate

Conversation

@bhowmickkrishnendu

@bhowmickkrishnendu bhowmickkrishnendu commented Jun 21, 2025 •

Copy link
Copy Markdown

Summary

This PR adds support for a new validate key in vars_prompt, allowing regex-based input validation directly in playbooks.

Feature

Example usage:

- hosts: localhost
  gather_facts: no
  vars_prompt:
    - name: phone
      prompt: "Enter your 10-digit phone number"
      validate: "^[0-9]{10}$"
      private: no

  tasks:
    - debug:
        msg: "You entered: {{ phone }}"

If the user input does not match the regex, they are prompted again until the input is valid.

Changes Made

lib/ansible/playbook/play.py: Added 'validate' to the list of allowed keys in vars_prompt.

lib/ansible/executor/playbook_executor.py: Added logic to evaluate the regex and reprompt on failure.

Notes

Backward compatible: no changes to existing behavior unless validate is explicitly used.
Useful for enforcing specific input formats (e.g., phone numbers, IPs, usernames).

ISSUE TYPE

  • Feature Pull Request

@ansibot ansibot added needs_triage Needs a first human triage before being processed. ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jun 21, 2025
@ansibot

ansibot commented Jun 21, 2025

Copy link
Copy Markdown
Contributor

The test ansible-test sanity --test pep8 [explain] failed with 1 error:

lib/ansible/executor/playbook_executor.py:152:161: E501: line too long (161 > 160 characters)

click here for bot help

@ansibot ansibot removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. ci_verified Changes made in this PR are causing tests to fail. labels Jun 22, 2025
@bhowmickkrishnendu

Copy link
Copy Markdown
Author

@felixfontein

@felixfontein

Copy link
Copy Markdown
Contributor

@bhowmickkrishnendu why are you pining me in this PR?

@bhowmickkrishnendu

Copy link
Copy Markdown
Author

@bhowmickkrishnendu why are you pining me in this PR?

Hi @felixfontein , apologies for tagging you directly — I wasn’t aware of the review workflow in the Ansible community and was just trying to request feedback from someone experienced. Thank you for pointing it out!

I’ll follow the standard process and wait for maintainers or codeowners to review this PR. Appreciate your time and support!

@felixfontein

Copy link
Copy Markdown
Contributor

The core team usually goes through all (new) PRs once or twice a week, I think, but usually not on weekends.

@bhowmickkrishnendu

Copy link
Copy Markdown
Author

The core team usually goes through all (new) PRs once or twice a week, I think, but usually not on weekends.

Thanks for the clarification, @felixfontein — that’s really helpful. I’ll wait patiently for the core team to review it during the usual cycle. Appreciate your time!

@bcoca

bcoca commented Jun 23, 2025 •

Copy link
Copy Markdown
Member

how would this interact with overriding extra vars?

also, using assert/fail seems like a more complete solution

@bhowmickkrishnendu

Copy link
Copy Markdown
Author

how would this interact with overriding extra vars?

also, using assert/fail seems like a more complete solution

Thanks @bcoca — excellent points!

I tested the behavior with --extra-vars, and found that the prompt is still triggered even if the variable is already passed. I plan to update the logic to skip prompting and validation if the variable is already set, ensuring it behaves consistently with other Ansible variable precedence rules.

Regarding assert/fail, my intent was to bring regex validation closer to the input stage itself — allowing early feedback during prompts, before the play begins. But I totally understand your point, and am open to feedback if there's a preferred way you'd like this handled.

I’ll push a patch shortly with the fix for --extra-vars. Appreciate your review and guidance!

@bhowmickkrishnendu

Copy link
Copy Markdown
Author

@bcoca Thanks for the suggestion! Now validation also applies to values passed via --extra-vars or other sources. If the input doesn't match the validate: regex, it raises an error and avoids prompting.

@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label Jun 26, 2025
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jul 3, 2025
root added 3 commits July 4, 2025 01:15
Signed-off-by: root <root@my-multipurpose-srv.subnet12291634.vcn12291634.oraclevcn.com>
Signed-off-by: root <root@my-multipurpose-srv.subnet12291634.vcn12291634.oraclevcn.com>
@bhowmickkrishnendu
bhowmickkrishnendu force-pushed the feature-vars-prompt-validate branch from 455423c to c6d900b Compare July 4, 2025 01:16
@bhowmickkrishnendu

Copy link
Copy Markdown
Author

Rebased with latest devel to keep CI up-to-date. Looking forward to review feedback!

@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jul 4, 2025
@bhowmickkrishnendu

Copy link
Copy Markdown
Author

All requested changes are now complete, and I've added the changelog fragment as per the contribution guidelines.
This PR is ready for review. Thank you!

@webknjaz

Copy link
Copy Markdown
Member

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jul 25, 2025
@bhowmickkrishnendu

Copy link
Copy Markdown
Author

Hi team 👋 Just checking in — it’s been a couple of weeks since /azp run, and the stale_ci label is now showing. Let me know if I should rebase or re-trigger CI. Happy to adjust anything needed 🙌

@ansibot ansibot added the stale_pr This PR has not been pushed to for more than one year. label Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. stale_pr This PR has not been pushed to for more than one year.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants