index.rst 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. .. meta::
  2. :description: YAML is a markup language with powerful features. YAML syntax may seem daunting, but there are only 3 simple rules to remember for writing YAML for SLS files.
  3. :keywords: yaml, what is yaml, how to use yaml
  4. .. _yaml:
  5. ==============================
  6. What is YAML and How To Use It
  7. ==============================
  8. The default renderer for SLS files is the YAML renderer.
  9. What is YAML
  10. ------------
  11. What does YAML stand for? It's an acronym for *YAML Ain't Markup Language*.
  12. `The Official YAML Website <https://yaml.org>`_ defines YAML as:
  13. *...a human friendly data serialization*
  14. *standard for all programming languages.*
  15. However, Salt uses a small subset of YAML that maps over very commonly used data
  16. structures, like lists and dictionaries. It is the job of the YAML renderer to
  17. take the YAML data structure and compile it into a Python data structure for use
  18. by Salt.
  19. Defining YAML
  20. -------------
  21. Though YAML syntax may seem daunting and terse at first, there are only
  22. three very simple rules to remember when writing YAML for SLS files.
  23. Rule One: Indentation
  24. +++++++++++++++++++++
  25. YAML uses a fixed indentation scheme to represent relationships between
  26. data layers. Salt requires that the indentation for each level consists
  27. of exactly two spaces. Do not use tabs.
  28. Rule Two: Colons
  29. ++++++++++++++++
  30. Python dictionaries are, of course, simply key-value pairs. Users from other
  31. languages may recognize this data type as hashes or associative arrays.
  32. Dictionary keys are represented in YAML as strings terminated by a trailing
  33. colon. Values are represented by either a string following the colon,
  34. separated by a space:
  35. .. code-block:: yaml
  36. my_key: my_value
  37. In Python, the above maps to:
  38. .. code-block:: python
  39. {"my_key": "my_value"}
  40. Alternatively, a value can be associated with a key through indentation.
  41. .. code-block:: yaml
  42. my_key:
  43. my_value
  44. .. note::
  45. The above syntax is valid YAML but is uncommon in SLS files because most often,
  46. the value for a key is not singular but instead is a *list* of values.
  47. In Python, the above maps to:
  48. .. code-block:: python
  49. {"my_key": "my_value"}
  50. Dictionaries can be nested:
  51. .. code-block:: yaml
  52. first_level_dict_key:
  53. second_level_dict_key: value_in_second_level_dict
  54. And in Python:
  55. .. code-block:: python
  56. {"first_level_dict_key": {"second_level_dict_key": "value_in_second_level_dict"}}
  57. Rule Three: Dashes
  58. ++++++++++++++++++
  59. To represent lists of items, a single dash followed by a space is used. Multiple
  60. items are a part of the same list as a function of their having the same level of indentation.
  61. .. code-block:: yaml
  62. - list_value_one
  63. - list_value_two
  64. - list_value_three
  65. Lists can be the value of a key-value pair. This is quite common in Salt:
  66. .. code-block:: yaml
  67. my_dictionary:
  68. - list_value_one
  69. - list_value_two
  70. - list_value_three
  71. In Python, the above maps to:
  72. .. code-block:: python
  73. {"my_dictionary": ["list_value_one", "list_value_two", "list_value_three"]}
  74. Learning more about YAML
  75. ------------------------
  76. One easy way to learn more about how YAML gets rendered into Python data structures is
  77. to use an online YAML parser to see the Python output.
  78. Here are some excellent links for experimenting with and referencing YAML:
  79. * `Online YAML Parser <https://yaml-online-parser.appspot.com/>`_: Convert YAML
  80. to JSON or Python data structures.
  81. * `The Official YAML Specification <https://yaml.org/spec/1.2/spec.html>`_
  82. * `The Wikipedia page for YAML <https://en.wikipedia.org/wiki/YAML>`_
  83. Templating
  84. ----------
  85. Jinja statements and expressions are allowed by default in SLS files. See
  86. :ref:`Understanding Jinja <understanding-jinja>`.