2016-12-15 21:03:56 +00:00
|
|
|
.. _yaml-idiosyncrasies:
|
|
|
|
|
2012-03-03 06:12:53 +00:00
|
|
|
===================
|
|
|
|
YAML Idiosyncrasies
|
|
|
|
===================
|
|
|
|
|
2012-03-18 23:18:47 +00:00
|
|
|
One of Salt's strengths, the use of existing serialization systems for
|
2012-05-23 04:43:12 +00:00
|
|
|
representing SLS data, can also backfire. `YAML`_ is a general purpose system
|
2012-03-03 06:12:53 +00:00
|
|
|
and there are a number of things that would seem to make sense in an sls
|
2012-03-18 23:18:47 +00:00
|
|
|
file that cause YAML issues. It is wise to be aware of these issues. While
|
2012-03-03 06:12:53 +00:00
|
|
|
reports or running into them are generally rare they can still crop up at
|
|
|
|
unexpected times.
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
.. _`YAML`: http://yaml.org/spec/1.1/
|
|
|
|
|
2012-03-03 06:12:53 +00:00
|
|
|
Spaces vs Tabs
|
|
|
|
==============
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
`YAML uses spaces`_, period. Do not use tabs in your SLS files! If strange
|
|
|
|
errors are coming up in rendering SLS files, make sure to check that
|
2012-11-19 01:22:41 +00:00
|
|
|
no tabs have crept in! In Vim, after enabling search highlighting
|
|
|
|
with: ``:set hlsearch``, you can check with the following key sequence in
|
|
|
|
normal mode(you can hit `ESC` twice to be sure): ``/``, `Ctrl-v`, `Tab`, then
|
|
|
|
hit `Enter`. Also, you can convert tabs to 2 spaces by these commands in Vim:
|
|
|
|
``:set tabstop=2 expandtab`` and then ``:retab``.
|
2012-03-03 06:12:53 +00:00
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
.. _`YAML uses spaces`: http://yaml.org/spec/1.1/#id871998
|
|
|
|
|
2012-03-03 06:12:53 +00:00
|
|
|
Indentation
|
|
|
|
===========
|
2012-03-18 23:18:47 +00:00
|
|
|
The suggested syntax for YAML files is to use 2 spaces for indentation,
|
|
|
|
but YAML will follow whatever indentation system that the individual file
|
2012-05-23 04:43:12 +00:00
|
|
|
uses. Indentation of two spaces works very well for SLS files given the
|
2012-03-18 23:18:47 +00:00
|
|
|
fact that the data is uniform and not deeply nested.
|
2012-03-03 06:12:53 +00:00
|
|
|
|
2013-11-02 04:02:29 +00:00
|
|
|
.. _nested-dict-indentation:
|
|
|
|
|
2014-11-09 05:27:36 +00:00
|
|
|
Nested Dictionaries
|
|
|
|
-------------------
|
2012-03-03 06:12:53 +00:00
|
|
|
|
2014-06-05 23:20:53 +00:00
|
|
|
When :ref:`dicts <python2:typesmapping>` are nested within other data
|
|
|
|
structures (particularly lists), the indentation logic sometimes changes.
|
|
|
|
Examples of where this might happen include ``context`` and ``default`` options
|
2016-12-15 22:36:44 +00:00
|
|
|
from the :mod:`file.managed <salt.states.file>` state:
|
2012-03-03 06:12:53 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
/etc/http/conf/http.conf:
|
|
|
|
file:
|
|
|
|
- managed
|
|
|
|
- source: salt://apache/http.conf
|
|
|
|
- user: root
|
|
|
|
- group: root
|
|
|
|
- mode: 644
|
|
|
|
- template: jinja
|
|
|
|
- context:
|
|
|
|
custom_var: "override"
|
|
|
|
- defaults:
|
|
|
|
custom_var: "default value"
|
|
|
|
other_var: 123
|
|
|
|
|
2013-11-02 04:02:29 +00:00
|
|
|
Notice that while the indentation is two spaces per level, for the values under
|
|
|
|
the ``context`` and ``defaults`` options there is a four-space indent. If only
|
2014-06-05 23:20:53 +00:00
|
|
|
two spaces are used to indent, then those keys will be considered part of the
|
|
|
|
same dictionary that contains the ``context`` key, and so the data will not be
|
2014-11-09 05:27:36 +00:00
|
|
|
loaded correctly. If using a double indent is not desirable, then a
|
|
|
|
deeply-nested dict can be declared with curly braces:
|
2012-03-03 06:12:53 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
/etc/http/conf/http.conf:
|
|
|
|
file:
|
|
|
|
- managed
|
|
|
|
- source: salt://apache/http.conf
|
|
|
|
- user: root
|
|
|
|
- group: root
|
|
|
|
- mode: 644
|
|
|
|
- template: jinja
|
|
|
|
- context: {
|
|
|
|
custom_var: "override" }
|
|
|
|
- defaults: {
|
2012-05-01 18:04:41 +00:00
|
|
|
custom_var: "default value",
|
2012-03-03 06:12:53 +00:00
|
|
|
other_var: 123 }
|
|
|
|
|
2014-06-05 23:20:53 +00:00
|
|
|
Here is a more concrete example of how YAML actually handles these
|
|
|
|
indentations, using the Python interpreter on the command line:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
>>> import yaml
|
|
|
|
>>> yaml.safe_load('''mystate:
|
|
|
|
... file.managed:
|
|
|
|
... - context:
|
|
|
|
... some: var''')
|
|
|
|
{'mystate': {'file.managed': [{'context': {'some': 'var'}}]}}
|
|
|
|
>>> yaml.safe_load('''mystate:
|
|
|
|
... file.managed:
|
|
|
|
... - context:
|
|
|
|
... some: var''')
|
|
|
|
{'mystate': {'file.managed': [{'some': 'var', 'context': None}]}}
|
|
|
|
|
|
|
|
Note that in the second example, ``some`` is added as another key in the same
|
|
|
|
dictionary, whereas in the first example, it's the start of a new dictionary.
|
|
|
|
That's the distinction. ``context`` is a common example because it is a keyword
|
|
|
|
arg for many functions, and should contain a dictionary.
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
|
2013-02-22 20:09:32 +00:00
|
|
|
True/False, Yes/No, On/Off
|
|
|
|
==========================
|
|
|
|
|
|
|
|
PyYAML will load these values as boolean ``True`` or ``False``. Un-capitalized
|
|
|
|
versions will also be loaded as booleans (``true``, ``false``, ``yes``, ``no``,
|
|
|
|
``on``, and ``off``). This can be especially problematic when constructing
|
|
|
|
Pillar data. Make sure that your Pillars which need to use the string versions
|
2015-10-22 18:39:14 +00:00
|
|
|
of these values are enclosed in quotes. Pillars will be parsed twice by salt,
|
|
|
|
so you'll need to wrap your values in multiple quotes, for example '"false"'.
|
2013-02-22 20:09:32 +00:00
|
|
|
|
2016-07-20 20:49:52 +00:00
|
|
|
The '%' Sign
|
|
|
|
============
|
|
|
|
|
|
|
|
The `%` symbol has a special meaning in YAML, it needs to be passed as a
|
|
|
|
string literal:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
cheese:
|
|
|
|
ssh_auth.present:
|
|
|
|
- user: tbortels
|
|
|
|
- source: salt://ssh_keys/chease.pub
|
|
|
|
- config: '%h/.ssh/authorized_keys'
|
|
|
|
|
2012-03-03 06:12:53 +00:00
|
|
|
Integers are Parsed as Integers
|
|
|
|
===============================
|
|
|
|
|
2012-08-20 21:38:38 +00:00
|
|
|
NOTE: This has been fixed in salt 0.10.0, as of this release passing an
|
2012-06-04 19:40:35 +00:00
|
|
|
integer that is preceded by a 0 will be correctly parsed
|
|
|
|
|
2013-07-23 17:43:23 +00:00
|
|
|
When passing :func:`integers <python2:int>` into an SLS file, they are
|
|
|
|
passed as integers. This means that if a state accepts a string value
|
|
|
|
and an integer is passed, that an integer will be sent. The solution here
|
|
|
|
is to send the integer as a string.
|
2012-03-03 06:12:53 +00:00
|
|
|
|
|
|
|
This is best explained when setting the mode for a file:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
/etc/vimrc:
|
|
|
|
file:
|
|
|
|
- managed
|
|
|
|
- source: salt://edit/vimrc
|
|
|
|
- user: root
|
|
|
|
- group: root
|
|
|
|
- mode: 644
|
|
|
|
|
|
|
|
Salt manages this well, since the mode is passed as 644, but if the mode is
|
2012-03-18 23:18:47 +00:00
|
|
|
zero padded as 0644, then it is read by YAML as an integer and evaluated as
|
2012-08-20 20:15:04 +00:00
|
|
|
an octal value, 0644 becomes 420. Therefore, if the file mode is
|
2012-03-03 06:12:53 +00:00
|
|
|
preceded by a 0 then it needs to be passed as a string:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
/etc/vimrc:
|
|
|
|
file:
|
|
|
|
- managed
|
|
|
|
- source: salt://edit/vimrc
|
|
|
|
- user: root
|
|
|
|
- group: root
|
|
|
|
- mode: '0644'
|
2013-07-09 16:12:38 +00:00
|
|
|
|
2012-04-23 06:33:57 +00:00
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
YAML does not like "Double Short Decs"
|
2012-04-23 06:33:57 +00:00
|
|
|
======================================
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
If I can find a way to make YAML accept "Double Short Decs" then I will, since
|
2012-04-23 06:33:57 +00:00
|
|
|
I think that double short decs would be awesome. So what is a "Double Short
|
|
|
|
Dec"? It is when you declare a multiple short decs in one ID. Here is a
|
|
|
|
standard short dec, it works great:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
vim:
|
|
|
|
pkg.installed
|
|
|
|
|
|
|
|
The short dec means that there are no arguments to pass, so it is not required
|
|
|
|
to add any arguments, and it can save space.
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
YAML though, gets upset when declaring multiple short decs, for the record...
|
2012-04-23 06:33:57 +00:00
|
|
|
|
|
|
|
THIS DOES NOT WORK:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
vim:
|
|
|
|
pkg.installed
|
|
|
|
user.present
|
|
|
|
|
|
|
|
Similarly declaring a short dec in the same ID dec as a standard dec does not
|
|
|
|
work either...
|
|
|
|
|
|
|
|
ALSO DOES NOT WORK:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
fred:
|
|
|
|
user.present
|
2012-11-19 01:22:41 +00:00
|
|
|
ssh_auth.present:
|
2012-04-23 06:33:57 +00:00
|
|
|
- name: AAAAB3NzaC...
|
2012-11-19 01:22:41 +00:00
|
|
|
- user: fred
|
|
|
|
- enc: ssh-dss
|
|
|
|
- require:
|
|
|
|
- user: fred
|
2012-04-23 06:33:57 +00:00
|
|
|
|
2012-11-19 01:22:41 +00:00
|
|
|
The correct way is to define them like this:
|
2012-04-23 06:33:57 +00:00
|
|
|
|
2012-11-19 01:22:41 +00:00
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
vim:
|
|
|
|
pkg.installed: []
|
|
|
|
user.present: []
|
|
|
|
|
|
|
|
fred:
|
|
|
|
user.present: []
|
|
|
|
ssh_auth.present:
|
|
|
|
- name: AAAAB3NzaC...
|
|
|
|
- user: fred
|
|
|
|
- enc: ssh-dss
|
|
|
|
- require:
|
|
|
|
- user: fred
|
|
|
|
|
|
|
|
|
|
|
|
Alternatively, they can be defined the "old way", or with multiple
|
|
|
|
"full decs":
|
2012-04-23 06:33:57 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
vim:
|
|
|
|
pkg:
|
|
|
|
- installed
|
|
|
|
user:
|
|
|
|
- present
|
|
|
|
|
|
|
|
fred:
|
|
|
|
user:
|
|
|
|
- present
|
2012-11-19 01:22:41 +00:00
|
|
|
ssh_auth:
|
|
|
|
- present
|
2012-04-23 06:33:57 +00:00
|
|
|
- name: AAAAB3NzaC...
|
2012-11-19 01:22:41 +00:00
|
|
|
- user: fred
|
|
|
|
- enc: ssh-dss
|
|
|
|
- require:
|
|
|
|
- user: fred
|
2012-04-23 06:33:57 +00:00
|
|
|
|
2012-08-29 13:56:12 +00:00
|
|
|
YAML support only plain ASCII
|
|
|
|
=============================
|
|
|
|
|
2012-08-29 16:41:39 +00:00
|
|
|
According to YAML specification, only ASCII characters can be used.
|
2012-08-29 13:56:12 +00:00
|
|
|
|
|
|
|
Within double-quotes, special characters may be represented with C-style
|
2012-08-29 17:06:19 +00:00
|
|
|
escape sequences starting with a backslash ( \\ ).
|
2012-08-29 13:56:12 +00:00
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2012-08-29 17:06:19 +00:00
|
|
|
.. code-block:: yaml
|
|
|
|
|
2012-08-29 13:56:12 +00:00
|
|
|
- micro: "\u00b5"
|
|
|
|
- copyright: "\u00A9"
|
|
|
|
- A: "\x41"
|
|
|
|
- alpha: "\u0251"
|
|
|
|
- Alef: "\u05d0"
|
|
|
|
|
2012-08-29 16:41:39 +00:00
|
|
|
|
2013-07-09 16:12:38 +00:00
|
|
|
|
2013-03-18 19:59:27 +00:00
|
|
|
List of usable `Unicode characters`_ will help you to identify correct numbers.
|
2012-08-29 17:06:19 +00:00
|
|
|
|
|
|
|
.. _`Unicode characters`: http://en.wikipedia.org/wiki/List_of_Unicode_characters
|
|
|
|
|
2012-08-29 13:56:12 +00:00
|
|
|
|
2012-08-29 16:41:39 +00:00
|
|
|
Python can also be used to discover the Unicode number for a character:
|
2012-08-29 13:56:12 +00:00
|
|
|
|
2012-08-29 17:06:19 +00:00
|
|
|
.. code-block:: python
|
|
|
|
|
2012-08-29 13:56:12 +00:00
|
|
|
repr(u"Text with wrong characters i need to figure out")
|
|
|
|
|
2012-08-29 16:41:39 +00:00
|
|
|
This shell command can find wrong characters in your SLS files:
|
2012-08-29 13:56:12 +00:00
|
|
|
|
2012-11-19 01:22:41 +00:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2012-08-29 13:56:12 +00:00
|
|
|
find . -name '*.sls' -exec grep --color='auto' -P -n '[^\x00-\x7F]' \{} \;
|
|
|
|
|
2013-07-09 16:11:43 +00:00
|
|
|
|
2014-01-23 14:09:03 +00:00
|
|
|
Alternatively you can toggle the `yaml_utf8` setting in your master configuration
|
2016-01-25 11:26:30 +00:00
|
|
|
file. This is still an experimental setting but it should manage the right
|
|
|
|
encoding conversion in salt after yaml states compilations.
|
2014-01-23 14:09:03 +00:00
|
|
|
|
2013-07-09 16:11:43 +00:00
|
|
|
Underscores stripped in Integer Definitions
|
|
|
|
===========================================
|
|
|
|
|
|
|
|
If a definition only includes numbers and underscores, it is parsed by YAML as
|
|
|
|
an integer and all underscores are stripped. To ensure the object becomes a
|
2013-07-23 20:42:15 +00:00
|
|
|
string, it should be surrounded by quotes. `More information here`_.
|
2013-07-09 16:11:43 +00:00
|
|
|
|
2013-07-23 20:42:15 +00:00
|
|
|
.. _`More information here`: http://stackoverflow.com/questions/2723321/snakeyaml-how-to-disable-underscore-stripping-when-parsing
|
2013-07-09 16:11:43 +00:00
|
|
|
|
|
|
|
Here's an example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
>>> import yaml
|
|
|
|
>>> yaml.safe_load('2013_05_10')
|
|
|
|
20130510
|
|
|
|
>>> yaml.safe_load('"2013_05_10"')
|
|
|
|
'2013_05_10'
|
2014-03-31 17:46:21 +00:00
|
|
|
|
|
|
|
Automatic ``datetime`` conversion
|
|
|
|
=================================
|
|
|
|
|
|
|
|
If there is a value in a YAML file formatted ``2014-01-20 14:23:23`` or
|
|
|
|
similar, YAML will automatically convert this to a Python ``datetime`` object.
|
|
|
|
These objects are not msgpack serializable, and so may break core salt
|
|
|
|
functionality. If values such as these are needed in a salt YAML file
|
|
|
|
(specifically a configuration file), they should be formatted with surrounding
|
|
|
|
strings to force YAML to serialize them as strings:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
>>> import yaml
|
|
|
|
>>> yaml.safe_load('2014-01-20 14:23:23')
|
|
|
|
datetime.datetime(2014, 1, 20, 14, 23, 23)
|
|
|
|
>>> yaml.safe_load('"2014-01-20 14:23:23"')
|
|
|
|
'2014-01-20 14:23:23'
|
2014-08-12 22:25:43 +00:00
|
|
|
|
|
|
|
Additionally, numbers formatted like ``XXXX-XX-XX`` will also be converted (or
|
|
|
|
YAML will attempt to convert them, and error out if it doesn't think the date
|
2014-08-13 23:33:06 +00:00
|
|
|
is a real one). Thus, for example, if a minion were to have an ID of
|
2014-08-12 22:25:43 +00:00
|
|
|
``4017-16-20`` the minion would not start because YAML would complain that the
|
|
|
|
date was out of range. The workaround is the same, surround the offending
|
|
|
|
string with quotes:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
>>> import yaml
|
|
|
|
>>> yaml.safe_load('4017-16-20')
|
|
|
|
Traceback (most recent call last):
|
|
|
|
File "<stdin>", line 1, in <module>
|
|
|
|
File "/usr/local/lib/python2.7/site-packages/yaml/__init__.py", line 93, in safe_load
|
|
|
|
return load(stream, SafeLoader)
|
|
|
|
File "/usr/local/lib/python2.7/site-packages/yaml/__init__.py", line 71, in load
|
|
|
|
return loader.get_single_data()
|
|
|
|
File "/usr/local/lib/python2.7/site-packages/yaml/constructor.py", line 39, in get_single_data
|
|
|
|
return self.construct_document(node)
|
|
|
|
File "/usr/local/lib/python2.7/site-packages/yaml/constructor.py", line 43, in construct_document
|
|
|
|
data = self.construct_object(node)
|
|
|
|
File "/usr/local/lib/python2.7/site-packages/yaml/constructor.py", line 88, in construct_object
|
|
|
|
data = constructor(self, node)
|
|
|
|
File "/usr/local/lib/python2.7/site-packages/yaml/constructor.py", line 312, in construct_yaml_timestamp
|
|
|
|
return datetime.date(year, month, day)
|
|
|
|
ValueError: month must be in 1..12
|
|
|
|
>>> yaml.safe_load('"4017-16-20"')
|
2015-10-22 18:39:14 +00:00
|
|
|
'4017-16-20'
|
2016-05-24 15:12:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
Keys Limited to 1024 Characters
|
|
|
|
===============================
|
|
|
|
|
|
|
|
Simple keys are limited to a single line and cannot be longer that 1024 characters.
|
|
|
|
This is a limitation from PyYaml, as seen in a comment in `PyYAML's code`_, and
|
|
|
|
applies to anything parsed by YAML in Salt.
|
|
|
|
|
|
|
|
.. _PyYAML's code: http://pyyaml.org/browser/pyyaml/trunk/lib/yaml/scanner.py#L91
|