Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve documentation on doc fragments #62219

Merged
merged 3 commits into from
Sep 13, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 35 additions & 6 deletions docs/docsite/rst/dev_guide/developing_modules_documenting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,41 @@ Documentation fragments

If you're writing multiple related modules, they may share common documentation, such as authentication details, file mode settings, ``notes:`` or ``seealso:`` entries. Rather than duplicate that information in each module's ``DOCUMENTATION`` block, you can save it once as a doc_fragment plugin and use it in each module's documentation. In Ansible, shared documentation fragments are contained in a ``ModuleDocFragment`` class in `lib/ansible/plugins/doc_fragments/ <https://github.com/ansible/ansible/tree/devel/lib/ansible/plugins/doc_fragments>`_. To include a documentation fragment, add ``extends_documentation_fragment: FRAGMENT_NAME`` in your module's documentation.

Modules should only use a doc_fragment if it will implement all of the interface documented there in
a manner that behaves the same as the existing modules which implement that fragment. The goal is
that any parameter listed in doc_fragments will behave identically when used in another module
implementing the doc_fragment.
Modules should only use items from a doc fragment if the module will implement all of the interface documented there in a manner that behaves the same as the existing modules which import that fragment. The goal is that items imported from the doc fragment will behave identically when used in another module that imports the doc fragment.

By default, only the ``DOCUMENTATION`` property from a doc fragment is inserted into the module documentation. It is possible to define additional properties in the doc fragment in order to import only certain parts of a doc fragment or mix and match as appropriate. If a property is defined in both the doc fragment and the module, the module value overrides the doc fragment.

Here is an example doc fragment named ``example_fragment.py``:

.. code-block:: python

class ModuleDocFragment(object):
# Standard documentation
DOCUMENTATION = r'''
options:
# options here
'''

# Additional section
OTHER = r'''
options:
# other options here
'''


To insert the contents of ``OTHER`` in a module:

.. code-block:: yaml+jinja

extends_documentation_fragment: example_fragment.other

Or use both :

.. code-block:: yaml+jinja

extends_documentation_fragment:
- example_fragment
- example_fragment.other
samdoran marked this conversation as resolved.
Show resolved Hide resolved

.. _note:
* Prior to Ansible 2.8, documentation fragments were kept in ``lib/ansible/utils/module_docs_fragments``.
Expand All @@ -306,8 +337,6 @@ For example, all AWS modules should include:
- ec2


You can find more examples by searching for ``extends_documentation_fragment`` under the Ansible source tree.

.. _examples_block:

EXAMPLES block
Expand Down