fixes saltstack/salt#65222 document using slots for contents via function call in file state modules

This commit is contained in:
nicholasmhughes 2023-09-25 13:52:46 -04:00 committed by Daniel Wozniak
parent f2121e5ade
commit 2304b355b1
2 changed files with 78 additions and 0 deletions

View file

@ -68,3 +68,71 @@ Here is an example of result parsing and appending:
file.copy:
- name: __slot__:salt:user.info(someuser).home ~ /subdirectory
- source: salt://somefile
Execution Module Returns as File Contents or Data
-------------------------------------------------
The following examples demonstrate how to use execution module returns as file
contents or data in Salt states. These examples show how to incorporate the
output of execution functions into file contents or data in the `file.managed`
and `file.serialize` states.
Content from Execution Modules
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can use the results of execution modules directly as file contents in Salt
states. This can be useful for dynamically generating file content based on the
output of execution functions.
**Example 1: Using `test.echo` Output as File Content**
The following Salt state uses the `test.echo` execution function to generate the
text "hello world." This output is then used as the content of the file
`/tmp/things.txt`:
.. code-block:: yaml
content-from-slots:
file.managed:
- name: /tmp/things.txt
- contents: __slot__:salt:test.echo("hello world")
**Example 2: Using Multiple `test.echo` Outputs as Appended Content**
In this example, two `test.echo` execution functions are used to generate
"hello" and "world" strings. These strings are then joined by newline characters
and then used as the content of the file `/tmp/things.txt`:
.. code-block:: yaml
content-from-multiple-slots:
file.managed:
- name: /tmp/things.txt
- contents:
- __slot__:salt:test.echo("hello")
- __slot__:salt:test.echo("world")
Serializing Data from Execution Modules
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You can also serialize data obtained from execution modules and write it to
files using Salt states. This allows you to capture and store structured data
for later use.
**Example: Serializing `grains.items()` Output to JSON**
In this example, the `grains.items()` execution function retrieves system
information. The obtained data is then serialized into JSON format and saved to
the file `/tmp/grains.json`:
.. code-block:: yaml
serialize-dataset-from-slots:
file.serialize:
- name: /tmp/grains.json
- serializer: json
- dataset: __slot__:salt:grains.items()
These examples showcase how to leverage Salt's flexibility to use execution
module returns as file contents or serialized data in your Salt states, allowing
for dynamic and customized configurations.

View file

@ -2668,6 +2668,11 @@ def managed(
be used instead. However, this will not work for binary files in
Salt releases before 2015.8.4.
.. note:: For information on using Salt Slots and how to incorporate
execution module returns into file content or data, refer to the
`Salt Slots documentation
<https://docs.saltproject.io/en/latest/topics/slots/index.html>`_.
contents_grains
.. versionadded:: 2014.7.0
@ -7881,6 +7886,11 @@ def serialize(
.. versionadded:: 2015.8.0
.. note:: For information on using Salt Slots and how to incorporate
execution module returns into file content or data, refer to the
`Salt Slots documentation
<https://docs.saltproject.io/en/latest/topics/slots/index.html>`_.
serializer (or formatter)
Write the data as this format. See the list of
:ref:`all-salt.serializers` for supported output formats.