A Python tool that automatically generates argument_specs.yml files for Ansible collections and roles. It analyzes your role's variables, tasks, and defaults to create comprehensive argument specifications that provide documentation and validation for your Ansible roles.
- Collection-Wide Processing: Process all roles in a collection automatically
- Single Role Mode: Generate specs for individual roles with interactive or automated modes
- Intelligent Type Inference: Detects types from default values (and path-like names/values)
- Variable Discovery: Extracts variables from
defaults/main.ymlordefaults/main/, tasks, and templates (vars/via--include-vars) - Secret Handling: Sets
no_log: truefor password/token/secret-like names - Smart Filtering: Excludes registered variables, private (
_-prefixed) variables, and Ansible built-ins - Multiple Entry Points: Supports roles with multiple task entry points
- Spec Preservation: Keeps curated types, choices, conditionals, and
no_logon regenerate - Safe Writes: Timestamped
.bakbackups before overwrite (disable with--no-backup) - Version Tracking: Automatically adds
version_addedfor newly discovered variables - Validation: Checks types, required/default conflicts, choices, elements, and conditionals
# Install from PyPI
pip install ansible-argument-spec-generator
# Or install from source
pip install -e .Requirements:
- Python 3.11+
- PyYAML and Jinja2 (automatically installed)
- Ansible Core 2.11+ (for using the generated specs)
After installation, you have access to these commands:
ansible-argument-spec-generatorgenerate-argument-spec(shorter alias)
# Process all roles in current collection
ansible-argument-spec-generator
# Process a single role interactively
ansible-argument-spec-generator --single-role
# Get help
ansible-argument-spec-generator --helpProcess all roles in a collection:
# Process all roles in current collection
ansible-argument-spec-generator
# Process specific collection path
ansible-argument-spec-generator --collection-path /path/to/collection
# List roles in collection
ansible-argument-spec-generator --list-roles
# Process specific role only
ansible-argument-spec-generator --role my_roleProcess individual roles:
# Interactive mode
ansible-argument-spec-generator --single-role
# Generate from defaults file
ansible-argument-spec-generator --single-role --from-defaults defaults/main.yml
# Generate from configuration file
ansible-argument-spec-generator --single-role --from-config config.yml# Default - final summary only
ansible-argument-spec-generator
# Basic info
ansible-argument-spec-generator -v
# Detailed processing
ansible-argument-spec-generator -vv
# Full debug output
ansible-argument-spec-generator -vvv
# Suppress all output (including summaries)
ansible-argument-spec-generator --quiet| Option | Description |
|---|---|
--single-role |
Process individual role instead of entire collection |
--collection-path PATH |
Path to collection root (default: current directory) |
--list-roles |
List all roles found in collection |
--role NAME |
Process only the specified role |
--from-defaults FILE |
Generate specs from defaults file |
--from-config FILE |
Generate from configuration file |
--output FILE |
Output file path (default: meta/argument_specs.yml) |
--validate-only |
Validate existing specs without generating |
--include-vars |
Include variables from vars/ as options (off by default) |
--exclude-variable NAME |
Omit an argument, including an existing stale option; repeat for multiple names |
--no-backup |
Skip timestamped .bak backup before overwrite |
--dry-run |
Preview output without writing files |
-q, --quiet |
Suppress all output including summaries |
-v, -vv, -vvv |
Verbosity levels (basic, detailed, debug) |
The tool analyzes your Ansible roles to automatically generate argument specifications:
- Discovers Variables: Extracts variables from
defaults/main.ymlordefaults/main/, task files, and templates (vars/only with--include-vars) - Infers Types: Automatically detects variable types based on naming patterns and default values
- Marks Secrets: Sets
no_log: truefor password/token/secret-like variable names - Detects Entry Points: Identifies multiple task entry points (main.yml, install.yml, etc.)
- Filters Variables: Excludes registered variables, private variables, and Ansible built-ins
- Preserves Curated Specs: Keeps existing types, choices, conditionals, and
no_logon regenerate - Generates Specs: Creates clean
argument_specs.ymlfiles (with timestamped backups by default)
For complex scenarios, create a configuration file:
entry_points:
main:
short_description: "Install and configure web application"
arguments:
app_name:
type: str
required: true
description: "Name of the application"
state:
type: str
default: "present"
choices: ["present", "absent", "started", "stopped"]
description: "Desired state"
app_port:
type: int
default: 8080
description: "Port number"
required_if:
- ["state", "present", ["app_name"]]The tool creates standard argument_specs.yml files:
---
argument_specs:
main:
short_description: "Auto-generated specs for webapp role"
options:
app_enabled:
description: "Enable application"
type: bool
default: true
app_password:
description: "Password for authentication"
type: str
default: changeme
no_log: true
config_path:
description: "Configuration file path"
type: path
default: /etc/myapp/config.yml
version_added: "1.1.0"
...The tool automatically extracts variables from multiple sources:
- Defaults: Ansible's selected
defaults/main.yml,main.yaml,main.json, or extensionlessmainfile; adefaults/main/directory loads its supported files recursively - Vars (optional): the selected
vars/mainfile or directory, using the same rules as defaults - Task Files: Jinja expressions, conditionals, loops, asserts, environment, tags, and static includes/imports (including nested paths and blocks)
- Templates: Jinja2 variables under
templates/ - Multiple Entry Points: standalone task files not included by others (plus
main)
Vars values may still be used for type inference when a variable appears in tasks/templates, even if --include-vars is off. These variables remain optional because the role supplies them before validation. Unselected sibling files in defaults/ and vars/ are not merged automatically.
Types come primarily from default values:
- Python
bool/int/float/list/dict→ matching Ansible types - Path-like names (
*_path,*_dir, …) with path-like values →type: path - List element types are inferred from list contents (
boolchecked beforeint) - Secret-like names (
password,token,secret, …) →no_log: true - Task/template inputs without a known value use
raw, so lists and dictionaries are not incorrectly validated as strings; existing curated types take precedence
Automatically excludes:
- Private variables (names starting with
_) - Previously registered results and all
set_factassignments along each entry point's include paths - Task/block-local variables, custom loop/index variables, and Jinja loop, macro, and assignment locals
- Ansible built-ins (
ansible_*, and exact names likeitem,loop,hostvars,inventory_hostname, …)
An input used only behind a default/d fallback or a defined-variable guard is
optional. An unguarded reference elsewhere still makes it required, and an
explicit assert that an input is defined is treated as a requirement. Existing
curated required settings take precedence. Templates are analyzed with the
scope of their rendering task; unreferenced templates contribute optional inputs.
Existing options are preserved during regeneration, including names that older versions mistakenly discovered. Remove known stale arguments explicitly:
ansible-argument-spec-generator --role my_role \
--exclude-variable package_entry --exclude-variable package_indexExclusions apply to both newly discovered and existing options. Use --dry-run to skip writes; normal writes retain the existing backup behavior.
Analysis parses Jinja without executing lookups, custom filters, or tests. Static include paths are followed with their calling scope. File names computed at runtime cannot be resolved statically; review those roles' generated options.
Validate existing specs:
# Validate all roles
ansible-argument-spec-generator --validate-only
# Validate single role
ansible-argument-spec-generator --single-role --validate-onlyGenerated specs provide:
- Documentation:
ansible-doc --type role my_collection.my_role - Validation: Automatic argument validation
- Error Messages: Clear feedback for invalid inputs
# Process entire collection
cd /path/to/my_collection
ansible-argument-spec-generator
# Process single role in collection
ansible-argument-spec-generator --role webapp
# Include vars/ as options and skip backups
ansible-argument-spec-generator --include-vars --no-backup
# Preview without writing files
ansible-argument-spec-generator --dry-run
# Interactive single role mode
ansible-argument-spec-generator --single-role
# Generate from defaults file
ansible-argument-spec-generator --single-role --from-defaults defaults/main.yml- "Not a collection root": Ensure you're in a directory with
galaxy.ymlandroles/ - "No roles found": Check that
roles/directory contains valid role structures - YAML parsing errors: The tool provides specific error messages for malformed files
- File encoding issues: Ensure all files are UTF-8 encoded
Use verbosity flags for troubleshooting:
# List roles in collection
ansible-argument-spec-generator --list-roles
# Validate existing specs
ansible-argument-spec-generator --validate-only
# Debug with verbosity
ansible-argument-spec-generator -vvv --role myroleWe welcome contributions! Here's how you can help improve the Ansible Argument Specs Generator:
-
Clone the repository:
git clone https://github.com/djdanielsson/ansible_arg_spec_generator.git cd ansible_arg_spec_generator -
Set up development environment:
# Install Python 3.11+ python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate pip install -e ".[dev]"
-
Run tests:
# Run all tests pytest # Run with coverage pytest --cov=generate_argument_specs --cov-report=html # Run specific test categories pytest -k "test_basic"
-
Code formatting:
# Format code with Black black . # Check formatting black --check .
- Code Style: Follow PEP 8 guidelines
- Formatting: Use Black for consistent formatting
- Testing: Write tests for new features and bug fixes
- Documentation: Update README and docstrings for changes
- Commits: Use clear, descriptive commit messages
The project includes comprehensive tests covering:
- Core functionality
- Edge cases
- Integration tests
- CI/CD workflows
Run the full test suite:
pytest tests/ -v- Fork the repository
- Create a feature branch:
git checkout -b feature/your-feature - Make your changes and add tests
- Ensure all tests pass:
pytest - Format code:
black . - Commit your changes:
git commit -m "Add your feature" - Push to your fork:
git push origin feature/your-feature - Create a Pull Request
- Bug Reports: Use GitHub Issues with detailed reproduction steps
- Feature Requests: Describe the proposed feature and its use case
- Questions: Check existing issues or create a discussion
This project follows a code of conduct to ensure a welcoming environment for all contributors.
MIT