|
| 1 | +.. _play_argument_validation: |
| 2 | + |
| 3 | +Play Argument Validation |
| 4 | +------------------------ |
| 5 | + |
| 6 | +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. |
| 7 | + |
| 8 | +Play argument validation has two main parts: |
| 9 | + |
| 10 | +* The ``validate_argspec`` keyword. |
| 11 | +* A ``.meta.yml`` file that defines argument specifications. |
| 12 | + |
| 13 | +To enable play argument validation you: |
| 14 | + |
| 15 | +#. Define the argument specification identifier by setting a value for the ``validate_argspec`` keyword. You can set the value to ``True`` to use the play ``name`` or you can set the value to a string. |
| 16 | +#. Provide a valid argument specification in a ``<playbook_name>.meta.yml`` file in the same directory as the playbook. |
| 17 | + |
| 18 | +The following example provides a valid, empty argument specification named ``setup webserver`` for the playbook ``create_webserver.yml``: |
| 19 | + |
| 20 | +.. code-block:: yaml |
| 21 | +
|
| 22 | + # create_webserver.meta.yml |
| 23 | + argument_specs: |
| 24 | + setup webserver: |
| 25 | + options: {} |
| 26 | +
|
| 27 | +In the following playbook, both plays validate the play arguments against the ``setup webserver`` arguments: |
| 28 | + |
| 29 | +.. code-block:: yaml |
| 30 | +
|
| 31 | + # create_webserver.yml |
| 32 | + - name: setup webserver |
| 33 | + hosts: all |
| 34 | + validate_argspec: True |
| 35 | +
|
| 36 | + - hosts: all |
| 37 | + validate_argspec: setup webserver |
| 38 | +
|
| 39 | +Specification Format |
| 40 | +-------------------- |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +:argument-spec-name: |
| 45 | + |
| 46 | + * The name of the play or argument specification. |
| 47 | + |
| 48 | + :description: |
| 49 | + |
| 50 | + * A description of the play that may contain multiple lines. |
| 51 | + * This can be a single string or a list of strings. |
| 52 | + |
| 53 | + :options: |
| 54 | + |
| 55 | + * This section defines the dictionary of play arguments. |
| 56 | + * For each play option (argument), you may include: |
| 57 | + |
| 58 | + :option-name: |
| 59 | + |
| 60 | + * The name of the option/argument (required). |
| 61 | + |
| 62 | + :description: |
| 63 | + |
| 64 | + * Detailed explanation of what this option does. It should be written in full sentences. |
| 65 | + * This can be a single string or a list of strings. |
| 66 | + |
| 67 | + :type: |
| 68 | + |
| 69 | + * The data type of the option. See :ref:`Argument spec <argument_spec>` for allowed values for ``type``. The default is ``str``. |
| 70 | + * If an option is of type ``list``, ``elements`` should be specified. |
| 71 | + |
| 72 | + :required: |
| 73 | + |
| 74 | + * Only needed if ``true``. |
| 75 | + * If missing, the option is not required. |
| 76 | + |
| 77 | + :choices: |
| 78 | + |
| 79 | + * List of option values. |
| 80 | + * Should be absent if empty. |
| 81 | + |
| 82 | + :elements: |
| 83 | + |
| 84 | + * Specifies the data type for list elements when the type is ``list``. |
| 85 | + |
| 86 | + :options: |
| 87 | + |
| 88 | + * If this option takes a dict or list of dicts, you can define the structure here. |
| 89 | + |
| 90 | +Sample Specification |
| 91 | +-------------------- |
| 92 | + |
| 93 | +.. code-block:: yaml |
| 94 | +
|
| 95 | + # create_webservers.meta.yml |
| 96 | + description: Set up basic HTTPS-enabled webserver to serve content from a specified document root. |
| 97 | + argument_specs: |
| 98 | + setup webserver: |
| 99 | + options: |
| 100 | + document_root: |
| 101 | + description: Path to the directory containing static web content to be served. |
| 102 | + type: str |
| 103 | + required: True |
| 104 | + port: |
| 105 | + description: |
| 106 | + - Port number on which the webserver listens for incoming HTTPS connections. |
| 107 | + - When unspecified, the port is 443. |
| 108 | + type: int |
| 109 | + ssl_cert_path: |
| 110 | + description: Path to the SSL certificate. |
| 111 | + type: str |
| 112 | + required: True |
| 113 | + ssl_key_path: |
| 114 | + description: Path to the private key corresponding to the SSL certificate. |
| 115 | + type: str |
| 116 | + required: True |
0 commit comments