1
0

index.rst 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. {
  47. 'first_level_dict_key': {
  48. 'second_level_dict_key': 'value_in_second_level_dict'
  49. }
  50. }
  51. Rule Three: Dashes
  52. ------------------
  53. To represent lists of items, a single dash followed by a space is used. Multiple
  54. items are a part of the same list as a function of their having the same level of indentation.
  55. .. code-block:: yaml
  56. - list_value_one
  57. - list_value_two
  58. - list_value_three
  59. Lists can be the value of a key-value pair. This is quite common in Salt:
  60. .. code-block:: yaml
  61. my_dictionary:
  62. - list_value_one
  63. - list_value_two
  64. - list_value_three
  65. In Python, the above maps to:
  66. .. code-block:: python
  67. {'my_dictionary': ['list_value_one', 'list_value_two', 'list_value_three']}
  68. Learning More
  69. -------------
  70. One easy way to learn more about how YAML gets rendered into Python data structures is
  71. to use an online YAML parser to see the Python output.
  72. One excellent choice for experimenting with YAML parsing is: http://yaml-online-parser.appspot.com/
  73. Templating
  74. ----------
  75. Jinja statements and expressions are allowed by default in SLS files. See
  76. :ref:`Understanding Jinja <understanding-jinja>`.