index.rst 2.9 KB

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