Skip to main content

ansible-specdoc

A utility for dynamically generating documentation from an Ansible module's spec.

This project was primarily designed for the Linode Ansible Collection.

An example Ansible Collection using ansible-specdoc can be found here.

Security Warning

Do not use this tool in automation or with untrusted input sources.

This tool imports and executes arbitrary Python code from the input file provided with -i/--input_file or from stdin via --stdin. Any input source can result in arbitrary code execution. Only use this tool with trusted code and in a safe environment.

Usage

ansible-specdoc [-h] [-s] [-n MODULE_NAME] [-i INPUT_FILE] [-o OUTPUT_FILE] [-f {yaml,json,jinja2}] [-j] [-t TEMPLATE_FILE]

Generate Ansible Module documentation from spec.

options:
  -h, --help            show this help message and exit
  -s, --stdin           Read the module from stdin.
  -n MODULE_NAME, --module-name MODULE_NAME
                        The name of the module (required for stdin)
  -i INPUT_FILE, --input_file INPUT_FILE
                        The module to generate documentation from.
  -o OUTPUT_FILE, --output_file OUTPUT_FILE
                        The file to output the documentation to.
  -f {yaml,json,jinja2}, --output_format {yaml,json,jinja2}
                        The output format of the documentation.
  -j, --inject          Inject the output documentation into the `DOCUMENTATION`, `RETURN`, and `EXAMPLES` fields of input module.
  -t TEMPLATE_FILE, --template_file TEMPLATE_FILE
                        The file to use as the template for templated formats.
  -c, --clear_injected_fields,
                        Clears the DOCUMENTATION, RETURNS, and EXAMPLES fields in specified module and sets them to an empty string.

Generating a templated documentation file:

ansible-specdoc -f jinja2 -t path/to/my/template.md.j2 -i path/to/my/module.py -o path/to/output/file.md

Dynamically generating and injecting documentation back into module constants:

ansible-specdoc -j -i path/to/my/module.py

NOTE: Documentation string injection requires that you have DOCUMENTATION, RETURN, and EXAMPLES constants defined in your module.


ansible-specdoc -f yaml -i path/to/my/module.py

Implementation

Importing SpecDoc Classes

All of the ansible-specdoc classes can be imported into an Ansible module using the following statement:

from ansible_specdoc.objects import *

Alternatively, only specific classes can be imported using the following statement:

from ansible_specdoc.objects import SpecDocMeta, SpecField, SpecReturnValue, FieldType, DeprecationInfo

Declaring Module Metadata

The ansible-specdoc specification format requires that each module exports a SPECDOC_META object with the following structure:

SPECDOC_META = SpecDocMeta(
    description=['Module Description'],
    requirements=['python >= 3.6'],
    author=['Author Name'],
    options=module_spec,
    examples=[
        'example module usage'
    ],
    return_values={
        'my_return_value': SpecReturnValue(
            description='A generic return value.',
            type=FieldType.string,
            sample=['sample response']
        ),
    }
)

Declaring Argument Specification

Each SpecField object translates to a parameter that can be rendered into documentation and passed into Ansible for specification. These fields should be declared in a dict format as shown below:

module_spec = {
    'example_argument': SpecField(
        type=FieldType.string,
        required=True,
        description=['An example argument.']
    )
}

This dict should be passed into the options field of the SPECDOC_META declaration.

Passing Specification to Ansible

In order to retrieve the Ansible-compatible spec dict, use the SPECDOC_META.ansible_spec property.

Other Notes

To prevent ansible-specdoc from executing module code, please ensure that all module logic executes using the following pattern:

if __name__ == '__main__':
    main()

To deprecate a module, specify the deprecated field as follows:

SPECDOC_META = SpecDocMeta(
    ...
    deprecated=DeprecationInfo(
        alternative='my.new.module',
        removed_in='1.0.0',
        why='Reason for deprecation'
    )
)

When deprecating a module, you will also need to update your meta/runtime.yml file. Please refer to the official Ansible deprecation documentation for more details.

Templates

This repository provides an example Markdown template that can be used in conjunction with the -t argument.

Metadata

Release files for ansible-specdoc 0.0.20

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ansible-specdoc 0.0.20
File Size Uploaded Provenance Actions
ansible_specdoc-0.0.20.tar.gz 16.2 kB With provenance Details for ansible_specdoc-0.0.20.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ansible-specdoc 0.0.20
File Interpreter ABI Platform Actions
ansible_specdoc-0.0.20-py3-none-any.whl Python 3 none any Details for ansible_specdoc-0.0.20-py3-none-any.whl

Total release size: 29.4 kB

Release files / ansible_specdoc-0.0.20.tar.gz

Download URL ansible_specdoc-0.0.20.tar.gz
Size 16.2 kB
Tags Source
SHA-256 checksum
How to use checksums
179c3c2804ddfd7214afd0f67d38694d7bc971b989279ba432f838d5d5876083
BLAKE2b-256 checksum
How to use checksums
957a031efbbd0583cc925c297a87ed943148ee96a1a6a412dd1cd67a65461044
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.

Transparency log

Release files / ansible_specdoc-0.0.20-py3-none-any.whl

Download URL ansible_specdoc-0.0.20-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d9662f599d6b1b1ca3b2af4d2541d89a4b00342e6781ae790101816e495d991e
BLAKE2b-256 checksum
How to use checksums
7207f743ec525dd69629a4237a0f8d03855b88611a76f09d8d00adc06defa358
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.

Transparency log

Security for ansible-specdoc 0.0.20

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for ansible_specdoc-0.0.20.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page