2016-06-14 00:30:42 +00:00
|
|
|
.. _yaml:
|
|
|
|
|
|
|
|
==================
|
2014-02-13 00:15:05 +00:00
|
|
|
Understanding YAML
|
|
|
|
==================
|
|
|
|
|
|
|
|
The default renderer for SLS files is the YAML renderer. YAML is a
|
|
|
|
markup language with many powerful features. However, Salt uses
|
2014-04-30 20:55:30 +00:00
|
|
|
a small subset of YAML that maps over very commonly used data structures,
|
2014-02-13 00:15:05 +00:00
|
|
|
like lists and dictionaries. It is the job of the YAML renderer to take
|
|
|
|
the YAML data structure and compile it into a Python data structure for
|
|
|
|
use by Salt.
|
|
|
|
|
|
|
|
Though YAML syntax may seem daunting and terse at first, there are only
|
|
|
|
three very simple rules to remember when writing YAML for SLS files.
|
|
|
|
|
|
|
|
Rule One: Indentation
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
YAML uses a fixed indentation scheme to represent relationships between
|
|
|
|
data layers. Salt requires that the indentation for each level consists
|
|
|
|
of exactly two spaces. Do not use tabs.
|
|
|
|
|
|
|
|
|
|
|
|
Rule Two: Colons
|
|
|
|
----------------
|
|
|
|
|
|
|
|
Python dictionaries are, of course, simply key-value pairs. Users from other
|
|
|
|
languages may recognize this data type as hashes or associative arrays.
|
|
|
|
|
2014-02-19 04:18:10 +00:00
|
|
|
Dictionary keys are represented in YAML as strings terminated by a trailing
|
|
|
|
colon. Values are represented by either a string following the colon,
|
|
|
|
separated by a space:
|
2014-02-13 00:15:05 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
2014-02-19 04:18:10 +00:00
|
|
|
|
2014-02-13 00:15:05 +00:00
|
|
|
my_key: my_value
|
|
|
|
|
|
|
|
In Python, the above maps to:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2014-02-19 04:18:10 +00:00
|
|
|
|
2014-02-13 00:15:05 +00:00
|
|
|
{'my_key': 'my_value'}
|
|
|
|
|
|
|
|
Alternatively, a value can be associated with a key through indentation.
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
2014-02-19 04:18:10 +00:00
|
|
|
|
2014-02-13 00:15:05 +00:00
|
|
|
my_key:
|
|
|
|
my_value
|
|
|
|
|
|
|
|
.. note::
|
2014-02-19 04:18:10 +00:00
|
|
|
|
2014-02-13 00:15:05 +00:00
|
|
|
The above syntax is valid YAML but is uncommon in SLS files because most often,
|
|
|
|
the value for a key is not singular but instead is a *list* of values.
|
|
|
|
|
|
|
|
In Python, the above maps to:
|
2014-02-13 16:26:25 +00:00
|
|
|
|
2014-03-16 08:01:21 +00:00
|
|
|
.. code-block:: python
|
|
|
|
|
2014-02-13 00:15:05 +00:00
|
|
|
{'my_key': 'my_value'}
|
|
|
|
|
|
|
|
Dictionaries can be nested:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
2014-02-19 04:18:10 +00:00
|
|
|
|
2014-02-13 00:15:05 +00:00
|
|
|
first_level_dict_key:
|
|
|
|
second_level_dict_key: value_in_second_level_dict
|
|
|
|
|
|
|
|
And in Python:
|
2014-02-19 04:18:10 +00:00
|
|
|
|
2014-02-13 00:15:05 +00:00
|
|
|
.. code-block:: python
|
2014-02-19 04:18:10 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
'first_level_dict_key': {
|
|
|
|
'second_level_dict_key': 'value_in_second_level_dict'
|
|
|
|
}
|
|
|
|
}
|
2014-02-13 00:15:05 +00:00
|
|
|
|
2014-03-15 00:43:03 +00:00
|
|
|
Rule Three: Dashes
|
2017-02-03 22:32:42 +00:00
|
|
|
------------------
|
2014-02-13 00:15:05 +00:00
|
|
|
|
|
|
|
To represent lists of items, a single dash followed by a space is used. Multiple
|
|
|
|
items are a part of the same list as a function of their having the same level of indentation.
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
2014-02-19 04:18:10 +00:00
|
|
|
|
2014-02-13 00:15:05 +00:00
|
|
|
- list_value_one
|
|
|
|
- list_value_two
|
|
|
|
- list_value_three
|
|
|
|
|
|
|
|
Lists can be the value of a key-value pair. This is quite common in Salt:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
my_dictionary:
|
|
|
|
- list_value_one
|
|
|
|
- list_value_two
|
|
|
|
- list_value_three
|
2014-04-01 11:59:42 +00:00
|
|
|
|
|
|
|
In Python, the above maps to:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
{'my_dictionary': ['list_value_one', 'list_value_two', 'list_value_three']}
|
2014-04-10 16:34:35 +00:00
|
|
|
|
|
|
|
Learning More
|
|
|
|
-------------
|
|
|
|
|
|
|
|
One easy way to learn more about how YAML gets rendered into Python data structures is
|
2014-12-11 15:51:43 +00:00
|
|
|
to use an online YAML parser to see the Python output.
|
2014-04-10 16:34:35 +00:00
|
|
|
|
2016-06-14 00:30:42 +00:00
|
|
|
One excellent choice for experimenting with YAML parsing is: http://yaml-online-parser.appspot.com/
|
|
|
|
|
|
|
|
Templating
|
|
|
|
----------
|
|
|
|
Jinja statements and expressions are allowed by default in SLS files. See
|
2017-02-03 22:32:42 +00:00
|
|
|
:ref:`Understanding Jinja <understanding-jinja>`.
|