[PR #10071/3249a041 backport][stable-10] Improve MH doc (#10074)
Some checks failed
EOL CI / EOL Sanity (Ⓐ2.15) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.15+py2.7) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.15+py3.10) (push) Has been cancelled
EOL CI / EOL Units (Ⓐ2.15+py3.5) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+alpine3+py:azp/posix/3/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/1/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/2/) (push) Has been cancelled
EOL CI / EOL I (Ⓐ2.15+fedora37+py:azp/posix/3/) (push) Has been cancelled
nox / Run extra sanity tests (push) Has been cancelled

Improve MH doc (#10071)

(cherry picked from commit 3249a041c0)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2025-04-27 09:50:24 +02:00 committed by GitHub
parent ac8942979b
commit 70bf4e449c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -76,13 +76,18 @@ section above, but there are more elements that will take part in it.
from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper from ansible_collections.community.general.plugins.module_utils.module_helper import ModuleHelper
class MyTest(ModuleHelper): class MyTest(ModuleHelper):
# behavior for module paramaters ONLY, see below for further information
output_params = () output_params = ()
change_params = () change_params = ()
diff_params = () diff_params = ()
facts_name = None
facts_params = () facts_params = ()
facts_name = None # used if generating facts, from parameters or otherwise
# transitional variables for the new VarDict implementation, see information below
use_old_vardict = True use_old_vardict = True
mute_vardict_deprecation = False mute_vardict_deprecation = False
module = dict( module = dict(
argument_spec=dict(...), argument_spec=dict(...),
# ... # ...
@ -211,9 +216,10 @@ One of the attributes in that metadata marks the variable for output, and MH mak
There are two ways to prevent that from happening: There are two ways to prevent that from happening:
#. Set ``mute_vardict_deprecation = True`` and the deprecation will be silenced. If the module still uses the old ``VarDict``, #. Set ``mute_vardict_deprecation = True`` and the deprecation will be silenced. If the module still uses the old ``VarDict``,
it will not be able to update to community.general 11.0.0 (Spring 2026) upon its release. it will not be able to update to community.general 11.0.0 (Spring 2025) upon its release.
#. Set ``use_old_vardict = False`` to make the MH module use the new ``VarDict`` immediatelly. #. Set ``use_old_vardict = False`` to make the MH module use the new ``VarDict`` immediately.
The new ``VarDict`` and its use is documented and this is the recommended way to handle this. We strongly recommend you use the new ``VarDict``, for that you make sure to consult its documentation at
:ref:`ansible_collections.community.general.docsite.guide_vardict`.
.. code-block:: python .. code-block:: python
@ -233,6 +239,11 @@ If you want to include some module parameters in the output, list them in the ``
output_params = ('state', 'name') output_params = ('state', 'name')
... ...
.. important::
The variable names listed in ``output_params`` **must be module parameters**, as in parameters listed in the module's ``argument_spec``.
Names not found in ``argument_spec`` are silently ignored.
Another neat feature provided by MH by using ``VarDict`` is the automatic tracking of changes when setting the metadata ``change=True``. Another neat feature provided by MH by using ``VarDict`` is the automatic tracking of changes when setting the metadata ``change=True``.
Again, to enable this feature for module parameters, you must list them in the ``change_params`` class variable. Again, to enable this feature for module parameters, you must list them in the ``change_params`` class variable.
@ -243,6 +254,11 @@ Again, to enable this feature for module parameters, you must list them in the `
change_params = ('value', ) change_params = ('value', )
... ...
.. important::
The variable names listed in ``change_params`` **must be module parameters**, as in parameters listed in the module's ``argument_spec``.
Names not found in ``argument_spec`` are silently ignored.
.. seealso:: .. seealso::
See more about this in See more about this in
@ -260,6 +276,11 @@ With that, MH will automatically generate the diff output for variables that hav
# example from community.general.gio_mime # example from community.general.gio_mime
self.vars.set_meta("handler", initial_value=gio_mime_get(self.runner, self.vars.mime_type), diff=True, change=True) self.vars.set_meta("handler", initial_value=gio_mime_get(self.runner, self.vars.mime_type), diff=True, change=True)
.. important::
The variable names listed in ``diff_params`` **must be module parameters**, as in parameters listed in the module's ``argument_spec``.
Names not found in ``argument_spec`` are silently ignored.
Moreover, if a module is set to return *facts* instead of return values, then again use the metadata ``fact=True`` and ``fact_params`` for module parameters. Moreover, if a module is set to return *facts* instead of return values, then again use the metadata ``fact=True`` and ``fact_params`` for module parameters.
Additionally, you must specify ``facts_name``, as in: Additionally, you must specify ``facts_name``, as in:
@ -283,6 +304,11 @@ That generates an Ansible fact like:
debug: debug:
msg: Volume fact is {{ ansible_facts.volume_facts.volume }} msg: Volume fact is {{ ansible_facts.volume_facts.volume }}
.. important::
The variable names listed in ``fact_params`` **must be module parameters**, as in parameters listed in the module's ``argument_spec``.
Names not found in ``argument_spec`` are silently ignored.
.. important:: .. important::
If ``facts_name`` is not set, the module does not generate any facts. If ``facts_name`` is not set, the module does not generate any facts.