Skip to main content

Ansible argument specification generator

This package contains code for Ansible argument specification program. Its main audience are Ansible module maintainers that would like to reduce the duplication in their modules by generating the argument specification directly from the module’s user documentation.

Quickstart

Documentation extractor is published on PyPI and we can install it using pip:

$ pip install ansible-argspec-gen[base]     # This will install ansible-base
$ pip install ansible-argspec-gen[ansible]  # This will install ansible
$ pip install ansible-argspec-gen           # We need to install ansible or
                                            # ansible-base ourselves

If the previous command did not fail, we are ready to start updating our modules. When we use the generator for the first time, we need to perform the following three steps:

  1. Add two comments to the module’s source that will mark the location for the generated code. By default, the generator searched for the # AUTOMATIC MODULE ARGUMENTS comment, but this can be changed with the --marker command-line parameter.

  2. Run the generator, possibly in dry-run and diff mode first to spot any issues.

  3. Remove any hand-writen remnants that are not needed anymore.

For example, let us assume that the first few lines of our module’s main function looks like this before the generator run:

def main():
    # AUTOMATIC MODULE ARGUMENTS
    # AUTOMATIC MODULE ARGUMENTS

    module = AnsibleModule(

If we run the the generator now in check mode with difference priting switched on, we will get back something like this:

$ ansible-argspec-gen --diff --dry-run plugins/modules/route.py
--- ../ansible_collections/steampunk/nginx_unit/plugins/modules/route.py.old
+++ ../ansible_collections/steampunk/nginx_unit/plugins/modules/route.py.new
@@ -359,6 +359,52 @@

 def main():
     # AUTOMATIC MODULE ARGUMENTS
+    argument_spec = {
+        "global": {"default": False, "type": "bool"},
+        "name": {"type": "str"},
+        "socket": {"type": "path"},
+        "state": {
+            "choices": ["present", "absent"],
+            "default": "present",
+            "type": "str",
+        },
+    }
+    required_if = [("global", False, ("name",)), ("state", "present", ("steps",))]
     # AUTOMATIC MODULE ARGUMENTS

     module = AnsibleModule(

Once we are happy wth the proposed changes, we can write them to the file:

$ ansible-argspec-gen plugins/modules/route.py

If we update the module’s documentation, we can simply rerun the previous command and generator will take or updating the specification. Note that the generator will overwrite the content between the markers, so make sure you do not manually modify that part of the file or you will loose the changes on next update.

Writing module documentation

Generating argument specification for the AnsibleModule class should work on any module that has a documentation. But getting the generator to produce other parameters such as conditional requirements takes a bit of work.

In order to generate a required_if specification, our parameters need to have a sentence in its description that fits the template required if I({param_name}) is C({param_value}). The next example:

options:
  name:
    description:
      - Name of the resource. Required if I(state) is C(present).

will produce the following specification:

required_if = [("state", "present", ("name", ))]

Another thing that generator knows how to produce is the mutually_exclusive specification. The pattern that the generator is looking for in this case is Mutually exclusive with I({param1}), I({param2}), and I({param3}), where the number of parameters that we can specify is not limited. Example:

options:
  processes:
    description:
      - Dynamic process limits.
      - Mutually exclusive with I(no_processes).
  no_processes:
    description:
      - Static process limit.
      - Mutually exclusive with I(processes).

This will produce:

mutually_exclusive = [("no_processes", "processes")]

Development setup

Getting development environment up and running is relatively simple if we have pipenv installed:

$ pipenv update

To test the extractor, we can run:

$ pipenv run ansible-argspec-gen

Metadata

Release files for ansible-argspec-gen 0.1.1

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-argspec-gen 0.1.1
File Size Uploaded
ansible-argspec-gen-0.1.1.tar.gz 19.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ansible-argspec-gen 0.1.1
File Interpreter ABI Platform
ansible_argspec_gen-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 38.6 kB

Release files / ansible-argspec-gen-0.1.1.tar.gz

Download URL ansible-argspec-gen-0.1.1.tar.gz
Size 19.4 kB
Tags Source
SHA-256 checksum
How to use checksums
3813c65cc63ff336776899a457f9a7343ab5887f1f8014710247beee5068e162
BLAKE2b-256 checksum
How to use checksums
de548792aabd046a96fd075463ea59b6c37db27825f0fd43b40b20360dd02068
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

Release files / ansible_argspec_gen-0.1.1-py3-none-any.whl

Download URL ansible_argspec_gen-0.1.1-py3-none-any.whl
Size 19.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a0ac6e29b4cf35620bf930cd556ada2c03adf73f169306ab2effce21b2bf4c7f
BLAKE2b-256 checksum
How to use checksums
6f7e9e53810d6cb83f061144d19e63882f7d74db9cd4659857736c143397b8a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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