| 1 |
.. |
| 2 |
Copyright (c) Ansible Project |
| 3 |
GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
.. _ansible_collections.community.general.docsite.guide_vardict: |
| 7 |
|
| 8 |
VarDict Guide |
| 9 |
============= |
| 10 |
|
| 11 |
Introduction |
| 12 |
^^^^^^^^^^^^ |
| 13 |
|
| 14 |
The ``ansible_collections.community.general.plugins.module_utils._vardict`` module util provides the |
| 15 |
``VarDict`` class to help manage the module variables. That class is a container for module variables, |
| 16 |
especially the ones for which the module must keep track of state changes, and the ones that should |
| 17 |
be published as return values. |
| 18 |
|
| 19 |
Each variable has extra behaviors controlled by associated metadata, simplifying the generation of |
| 20 |
output values from the module. |
| 21 |
|
| 22 |
Quickstart |
| 23 |
"""""""""" |
| 24 |
|
| 25 |
The simplest way of using ``VarDict`` is: |
| 26 |
|
| 27 |
.. code-block:: python |
| 28 |
|
| 29 |
from ansible_collections.community.general.plugins.module_utils._vardict import VarDict |
| 30 |
|
| 31 |
Then in ``main()``, or any other function called from there: |
| 32 |
|
| 33 |
.. code-block:: python |
| 34 |
|
| 35 |
vars = VarDict() |
| 36 |
|
| 37 |
# Next 3 statements are equivalent |
| 38 |
vars.abc = 123 |
| 39 |
vars["abc"] = 123 |
| 40 |
vars.set("abc", 123) |
| 41 |
|
| 42 |
vars.xyz = "bananas" |
| 43 |
vars.ghi = False |
| 44 |
|
| 45 |
And by the time the module is about to exit: |
| 46 |
|
| 47 |
.. code-block:: python |
| 48 |
|
| 49 |
results = vars.output() |
| 50 |
module.exit_json(**results) |
| 51 |
|
| 52 |
That makes the return value of the module: |
| 53 |
|
| 54 |
.. code-block:: json |
| 55 |
|
| 56 |
{ |
| 57 |
"abc": 123, |
| 58 |
"xyz": "bananas", |
| 59 |
"ghi": false |
| 60 |
} |
| 61 |
|
| 62 |
Metadata |
| 63 |
"""""""" |
| 64 |
|
| 65 |
The metadata values associated with each variable are: |
| 66 |
|
| 67 |
- ``output: bool`` - marks the variable for module output as a module return value. |
| 68 |
- ``fact: bool`` - marks the variable for module output as an Ansible fact. |
| 69 |
- ``verbosity: int`` - sets the minimum level of verbosity for which the variable will be included in the output. |
| 70 |
- ``change: bool`` - controls the detection of changes in the variable value. |
| 71 |
- ``initial_value: any`` - when using ``change`` and need to forcefully set an intial value to the variable. |
| 72 |
- ``diff: bool`` - used along with ``change``, this generates an Ansible-style diff ``dict``. |
| 73 |
|
| 74 |
See the sections below for more details on how to use the metadata. |
| 75 |
|
| 76 |
|
| 77 |
Using VarDict |
| 78 |
^^^^^^^^^^^^^ |
| 79 |
|
| 80 |
Basic Usage |
| 81 |
""""""""""" |
| 82 |
|
| 83 |
As shown above, variables can be accessed using the ``[]`` operator, as in a ``dict`` object, |
| 84 |
and also as an object attribute, such as ``vars.abc``. The form using the ``set()`` |
| 85 |
method is special in the sense that you can use it to set metadata values: |
| 86 |
|
| 87 |
.. code-block:: python |
| 88 |
|
| 89 |
vars.set("abc", 123, output=False) |
| 90 |
vars.set("abc", 123, output=True, change=True) |
| 91 |
|
| 92 |
Another way to set metadata after the variables have been created is: |
| 93 |
|
| 94 |
.. code-block:: python |
| 95 |
|
| 96 |
vars.set_meta("abc", output=False) |
| 97 |
vars.set_meta("abc", output=True, change=True, diff=True) |
| 98 |
|
| 99 |
You can use either operator and attribute forms to access the value of the variable. Other ways to |
| 100 |
access its value and its metadata are: |
| 101 |
|
| 102 |
.. code-block:: python |
| 103 |
|
| 104 |
print("abc value = {0}".format(vars.var("abc")["value"])) # get the value |
| 105 |
print("abc output? {0}".format(vars.get_meta("abc")["output"])) # get the metadata like this |
| 106 |
|
| 107 |
The names of methods, such as ``set``, ``get_meta``, ``output`` amongst others, are reserved and |
| 108 |
cannot be used as variable names. If you try to use a reserved name a ``ValueError`` exception |
| 109 |
is raised with the message "Name <var> is reserved". |
| 110 |
|
| 111 |
Generating output |
| 112 |
""""""""""""""""" |
| 113 |
|
| 114 |
By default, every variable create will be enable for output with minimum verbosity set to zero, in |
| 115 |
other words, they will always be in the output by default. |
| 116 |
|
| 117 |
You can control that when creating the variable for the first time or later in the code: |
| 118 |
|
| 119 |
.. code-block:: python |
| 120 |
|
| 121 |
vars.set("internal", x + 4, output=False) |
| 122 |
vars.set_meta("internal", output=False) |
| 123 |
|
| 124 |
You can also set the verbosity of some variable, like: |
| 125 |
|
| 126 |
.. code-block:: python |
| 127 |
|
| 128 |
vars.set("abc", x + 4) |
| 129 |
vars.set("debug_x", x, verbosity=3) |
| 130 |
|
| 131 |
results = vars.output(module._verbosity) |
| 132 |
module.exit_json(**results) |
| 133 |
|
| 134 |
If the module was invoked with verbosity lower than 3, then the output will only contain |
| 135 |
the variable ``abc``. If running at higher verbosity, as in ``ansible-playbook -vvv``, |
| 136 |
then the output will also contain ``debug_x``. |
| 137 |
|
| 138 |
Generating facts is very similar to regular output, but variables are not marked as facts by default. |
| 139 |
|
| 140 |
.. code-block:: python |
| 141 |
|
| 142 |
vars.set("modulefact", x + 4, fact=True) |
| 143 |
vars.set("debugfact", x, fact=True, verbosity=3) |
| 144 |
|
| 145 |
results = vars.output(module._verbosity) |
| 146 |
results["ansible_facts"] = {"module_name": vars.facts(module._verbosity)} |
| 147 |
module.exit_json(**results) |
| 148 |
|
| 149 |
Handling change |
| 150 |
""""""""""""""" |
| 151 |
|
| 152 |
You can use ``VarDict`` to determine whether variables have had their values changed. |
| 153 |
|
| 154 |
.. code-block:: python |
| 155 |
|
| 156 |
vars.set("abc", 42, change=True) |
| 157 |
vars.abc = 90 |
| 158 |
|
| 159 |
results = vars.output() |
| 160 |
results["changed"] = vars.has_changed |
| 161 |
module.exit_json(**results) |
| 162 |
|
| 163 |
If tracking changes in variables, you may want to present the difference between the initial and the final |
| 164 |
values of it. For that, you want to use: |
| 165 |
|
| 166 |
.. code-block:: python |
| 167 |
|
| 168 |
vars.set("abc", 42, change=True, diff=True) |
| 169 |
vars.abc = 90 |
| 170 |
|
| 171 |
results = vars.output() |
| 172 |
results["changed"] = vars.has_changed |
| 173 |
results["diff"] = vars.diff() |
| 174 |
module.exit_json(**results) |
| 175 |
|
| 176 |
.. versionadded:: 7.1.0 |