Skip to content

Commit a633774

Browse files
committed
Document play argument spec validation
1 parent 0c37383 commit a633774

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

docs/docsite/rst/playbook_guide/playbooks_intro.rst

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,116 @@ You can use `ansible-lint <https://ansible.readthedocs.io/projects/lint/>`_ for
153153
154154
The `ansible-lint default rules <https://ansible.readthedocs.io/projects/lint/rules/>`_ page describes each error.
155155

156+
.. _play_argument_validation:
157+
158+
Play Argument Validation
159+
------------------------
160+
161+
Beginning in version 2.20, you can enable argument validation using the play keyword ``validate_argspec``. This adds a :ansplugin:`validate_argument_spec <ansible.builtin.validate_argument_spec#module>` task following play-level fact gathering. This feature is tech preview.
162+
163+
To turn on validation for a play, set ``validate_argspec`` to ``True`` to use the play ``name`` as the argument specification identifier, or set ``validate_argspec`` to a string to use instead of the play ``name``.
164+
165+
When play argument validation is enabled, a corresponding ``<playbook_name>.meta.yml`` file is required in the same directory as the playbook, and must contain a valid argument specification. This is a valid, empty argument specification named ``setup webserver`` for the playbook ``create_webserver.yml``:
166+
167+
.. code-block:: yaml
168+
169+
# create_webserver.meta.yml
170+
argument_specs:
171+
setup webserver:
172+
options: {}
173+
174+
In the following playbook, both plays validate the play arguments against the ``setup webserver`` arguments:
175+
176+
.. code-block:: yaml
177+
178+
# create_webserver.yml
179+
- name: setup webserver
180+
hosts: all
181+
validate_argspec: True
182+
183+
- hosts: all
184+
validate_argspec: setup webserver
185+
186+
Specification Format
187+
--------------------
188+
189+
The play argument specification must be defined in a top-level ``argument_specs`` block within the playbook's ``.meta.yml`` file. All fields are lowercase.
190+
191+
:argument-spec-name:
192+
193+
* The name of the play or argument specification.
194+
195+
:description:
196+
197+
* A description of the play that may contain multiple lines.
198+
* This can be a single string or a list of strings.
199+
200+
:options:
201+
202+
* This section defines the dictionary of play arguments.
203+
* For each play option (argument), you may include:
204+
205+
:option-name:
206+
207+
* The name of the option/argument (required).
208+
209+
:description:
210+
211+
* Detailed explanation of what this option does. It should be written in full sentences.
212+
* This can be a single string or a list of strings.
213+
214+
:type:
215+
216+
* The data type of the option. See :ref:`Argument spec <argument_spec>` for allowed values for ``type``. The default is ``str``.
217+
* If an option is of type ``list``, ``elements`` should be specified.
218+
219+
:required:
220+
221+
* Only needed if ``true``.
222+
* If missing, the option is not required.
223+
224+
:choices:
225+
226+
* List of option values.
227+
* Should be absent if empty.
228+
229+
:elements:
230+
231+
* Specifies the data type for list elements when the type is ``list``.
232+
233+
:options:
234+
235+
* If this option takes a dict or list of dicts, you can define the structure here.
236+
237+
Sample Specification
238+
--------------------
239+
240+
.. code-block:: yaml
241+
242+
# create_webservers.meta.yml
243+
description: Set up basic HTTPS-enabled webserver to serve content from a specified document root.
244+
argument_specs:
245+
setup webserver:
246+
options:
247+
document_root:
248+
description: Path to the directory containing static web content to be served.
249+
type: str
250+
required: True
251+
port:
252+
description:
253+
- Port number on which the webserver listens for incoming HTTPS connections.
254+
- When unspecified, the port is 443.
255+
type: int
256+
ssl_cert_path:
257+
description: Path to the SSL certificate.
258+
type: str
259+
required: True
260+
ssl_key_path:
261+
description: Path to the private key corresponding to the SSL certificate.
262+
type: str
263+
required: True
264+
265+
156266
.. seealso::
157267

158268
`ansible-lint <https://ansible.readthedocs.io/projects/lint/>`_

0 commit comments

Comments
 (0)