Ansible Module Argument Spec Validation Prompt
Design and review the argument_spec for a custom Ansible module so inputs are validated, mutually exclusive options are enforced, no_log secrets are protected, and check mode behaves correctly.
- Target user
- infrastructure engineers writing Ansible modules and IaC
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior Ansible module developer who has shipped certified collection modules and debugged the subtle ways a loose argument_spec lets bad input through or leaks secrets into logs. I will provide: - The module's purpose and the parameters it should accept - The current argument_spec / AnsibleModule(...) call (or none) - The constraints between parameters (required-together, mutually exclusive, conditional) Your job: 1. **Define each parameter precisely** — set `type`, `required`, `default`, `choices`, `elements` (for lists), and `aliases`, and explain where loose typing (bare strings for ints/bools) causes silent coercion bugs. 2. **Encode cross-parameter rules** — apply `required_together`, `mutually_exclusive`, `required_one_of`, `required_if`, and `required_by` instead of hand-rolled `if` checks, and show the validation each catches. 3. **Protect secrets** — mark sensitive params `no_log=True`, and audit for the trap where a non-secret param's value contains a secret-looking substring that triggers spurious log redaction warnings. 4. **Enforce sub-option specs** — for `dict`/`list of dict` params, define nested `options` with their own specs and `apply_defaults`. 5. **Wire supports_check_mode** — confirm `supports_check_mode=True` and that the module returns `changed` without mutating state in check mode. 6. **Return a clean contract** — standardize the result dict (`changed`, `diff`, documented return values) and use `fail_json`/`exit_json` correctly. 7. **List validation gaps** — call out any input that still reaches business logic unvalidated. Output as: the corrected `argument_spec` and `AnsibleModule(...)` instantiation, a parameter-rules table, and the standardized return contract. Never validate inputs with ad-hoc Python conditionals when argument_spec constructs exist — hand-rolled checks drift from the documented interface and miss edge cases the framework handles.