ansible-specdoc


Nameansible-specdoc JSON
Version 0.0.14 PyPI version JSON
download
home_pagehttps://github.com/linode/ansible-specdoc/
SummaryA simple tool for generating Ansible collection documentation from module spec.
upload_time2023-07-20 13:26:31
maintainer
docs_urlNone
authorLinode
requires_python>=3
licenseApache License 2.0
keywords ansible
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ansible-specdoc

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

This project was primarily designed for the [Linode Ansible Collection](https://github.com/linode/ansible_linode).

An example Ansible Collection using `ansible-specdoc` can be found [here](https://github.com/linode/ansible-specdoc-example).

## Usage

```sh
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:

```shell
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:

```shell
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.

---

#### Generating a raw documentation string (not recommended):

```shell
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:

```python
from ansible_specdoc.objects import *
```

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

```python
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:

```python
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:

```python
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:

```python
if __name__ == '__main__':
    main()
```

---

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

```python
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](https://docs.ansible.com/ansible/latest/dev_guide/module_lifecycle.html#deprecating-modules-and-plugins-in-a-collection) for more details.

## Templates

This repository provides an [example Markdown template](./template/module.md.j2) that can be used in conjunction with the `-t` argument.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/linode/ansible-specdoc/",
    "name": "ansible-specdoc",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": "",
    "keywords": "ansible",
    "author": "Linode",
    "author_email": "dev-dx@linode.com",
    "download_url": "https://files.pythonhosted.org/packages/2f/13/096afbbaf4ccbca303398d592484f072cd9918c225e50f34252f1c04f800/ansible-specdoc-0.0.14.tar.gz",
    "platform": null,
    "description": "# ansible-specdoc\n\nA utility for dynamically generating documentation from an Ansible module's spec. \n\nThis project was primarily designed for the [Linode Ansible Collection](https://github.com/linode/ansible_linode).\n\nAn example Ansible Collection using `ansible-specdoc` can be found [here](https://github.com/linode/ansible-specdoc-example).\n\n## Usage\n\n```sh\nansible-specdoc [-h] [-s] [-n MODULE_NAME] [-i INPUT_FILE] [-o OUTPUT_FILE] [-f {yaml,json,jinja2}] [-j] [-t TEMPLATE_FILE]\n\nGenerate Ansible Module documentation from spec.\n\noptions:\n  -h, --help            show this help message and exit\n  -s, --stdin           Read the module from stdin.\n  -n MODULE_NAME, --module-name MODULE_NAME\n                        The name of the module (required for stdin)\n  -i INPUT_FILE, --input_file INPUT_FILE\n                        The module to generate documentation from.\n  -o OUTPUT_FILE, --output_file OUTPUT_FILE\n                        The file to output the documentation to.\n  -f {yaml,json,jinja2}, --output_format {yaml,json,jinja2}\n                        The output format of the documentation.\n  -j, --inject          Inject the output documentation into the `DOCUMENTATION`, `RETURN`, and `EXAMPLES` fields of input module.\n  -t TEMPLATE_FILE, --template_file TEMPLATE_FILE\n                        The file to use as the template for templated formats.\n  -c, --clear_injected_fields,\n                        Clears the DOCUMENTATION, RETURNS, and EXAMPLES fields in specified module and sets them to an empty string.\n```\n\n---\n\n#### Generating a templated documentation file:\n\n```shell\nansible-specdoc -f jinja2 -t path/to/my/template.md.j2 -i path/to/my/module.py -o path/to/output/file.md\n```\n\n---\n\n#### Dynamically generating and injecting documentation back into module constants:\n\n```shell\nansible-specdoc -j -i path/to/my/module.py\n```\n\nNOTE: Documentation string injection requires that you have `DOCUMENTATION`, `RETURN`, and `EXAMPLES` constants defined in your module.\n\n---\n\n#### Generating a raw documentation string (not recommended):\n\n```shell\nansible-specdoc -f yaml -i path/to/my/module.py\n```\n\n## Implementation\n\n### Importing SpecDoc Classes\n\nAll of the `ansible-specdoc` classes can be imported into an Ansible module using the following statement:\n\n```python\nfrom ansible_specdoc.objects import *\n```\n\nAlternatively, only specific classes can be imported using the following statement:\n\n```python\nfrom ansible_specdoc.objects import SpecDocMeta, SpecField, SpecReturnValue, FieldType, DeprecationInfo\n```\n\n### Declaring Module Metadata\nThe `ansible-specdoc` specification format requires that each module exports a `SPECDOC_META` object with the following structure:\n\n```python\nSPECDOC_META = SpecDocMeta(\n    description=['Module Description'],\n    requirements=['python >= 3.6'],\n    author=['Author Name'],\n    options=module_spec,\n    examples=[\n        'example module usage'\n    ],\n    return_values={\n        'my_return_value': SpecReturnValue(\n            description='A generic return value.',\n            type=FieldType.string,\n            sample=['sample response']\n        ),\n    }\n)\n```\n\n### Declaring Argument Specification\n\nEach `SpecField` object translates to a parameter that can be rendered into documentation and passed into Ansible for specification.\nThese fields should be declared in a dict format as shown below:\n\n```python\nmodule_spec = {\n    'example_argument': SpecField(\n        type=FieldType.string,\n        required=True,\n        description=['An example argument.']\n    )\n}\n```\n\nThis dict should be passed into the `options` field of the `SPECDOC_META` declaration.\n\n### Passing Specification to Ansible\n\nIn order to retrieve the Ansible-compatible spec dict, use the `SPECDOC_META.ansible_spec` property.\n\n### Other Notes\n\nTo prevent `ansible-specdoc` from executing module code, please ensure that all module logic executes using the following pattern:\n\n```python\nif __name__ == '__main__':\n    main()\n```\n\n---\n\nTo deprecate a module, specify the `deprecated` field as follows:\n\n```python\nSPECDOC_META = SpecDocMeta(\n    ...\n    deprecated=DeprecationInfo(\n        alternative='my.new.module',\n        removed_in='1.0.0',\n        why='Reason for deprecation'\n    )\n)\n```\n\nWhen deprecating a module, you will also need to update your `meta/runtime.yml` file.\nPlease refer to the [official Ansible deprecation documentation](https://docs.ansible.com/ansible/latest/dev_guide/module_lifecycle.html#deprecating-modules-and-plugins-in-a-collection) for more details.\n\n## Templates\n\nThis repository provides an [example Markdown template](./template/module.md.j2) that can be used in conjunction with the `-t` argument.\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "A simple tool for generating Ansible collection documentation from module spec.",
    "version": "0.0.14",
    "project_urls": {
        "Homepage": "https://github.com/linode/ansible-specdoc/"
    },
    "split_keywords": [
        "ansible"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b471de2dbeb02b56af635eebefa2d02563f1a81f19db9652a67fe016afee4146",
                "md5": "f088b6f38338cd473d6f772ffcd06b5e",
                "sha256": "6b787d9d8d8a2a902dfc55ab49f135b8ee0e54afe14ef7c8e77a2728696b01e3"
            },
            "downloads": -1,
            "filename": "ansible_specdoc-0.0.14-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "f088b6f38338cd473d6f772ffcd06b5e",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3",
            "size": 12470,
            "upload_time": "2023-07-20T13:26:30",
            "upload_time_iso_8601": "2023-07-20T13:26:30.266802Z",
            "url": "https://files.pythonhosted.org/packages/b4/71/de2dbeb02b56af635eebefa2d02563f1a81f19db9652a67fe016afee4146/ansible_specdoc-0.0.14-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2f13096afbbaf4ccbca303398d592484f072cd9918c225e50f34252f1c04f800",
                "md5": "9b86532cee5f9841f139f21f84438e52",
                "sha256": "dc6494015de56efd1c3435a6b300836b0a14b922a55699ece47ef759a82f4ec5"
            },
            "downloads": -1,
            "filename": "ansible-specdoc-0.0.14.tar.gz",
            "has_sig": false,
            "md5_digest": "9b86532cee5f9841f139f21f84438e52",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3",
            "size": 14565,
            "upload_time": "2023-07-20T13:26:31",
            "upload_time_iso_8601": "2023-07-20T13:26:31.769378Z",
            "url": "https://files.pythonhosted.org/packages/2f/13/096afbbaf4ccbca303398d592484f072cd9918c225e50f34252f1c04f800/ansible-specdoc-0.0.14.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-20 13:26:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "linode",
    "github_project": "ansible-specdoc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "ansible-specdoc"
}
        
Elapsed time: 0.09591s