vars.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. ===============================
  2. SLS Template Variable Reference
  3. ===============================
  4. .. warning::
  5. In the 3002 release ``sls_path``, ``tplfile``, and ``tpldir`` have had some significant
  6. improvements which have the potential to break states that rely on old and
  7. broken functionality. These fixes can be enabled by setting the
  8. ``enable_slsvars_fixes`` feature flag to ``True`` in your minion's config file.
  9. This functionality will become the default in the 3005 release.
  10. .. code-block:: yaml
  11. features:
  12. enable_slsvars_fixes: True
  13. The template engines available to sls files and file templates come loaded
  14. with a number of context variables. These variables contain information and
  15. functions to assist in the generation of templates. See each variable below
  16. for its availability -- not all variables are available in all templating
  17. contexts.
  18. Salt
  19. ====
  20. The `salt` variable is available to abstract the salt library functions. This
  21. variable is a python dictionary containing all of the functions available to
  22. the running salt minion. It is available in all salt templates.
  23. .. code-block:: jinja
  24. {% for file in salt['cmd.run']('ls -1 /opt/to_remove').splitlines() %}
  25. /opt/to_remove/{{ file }}:
  26. file.absent
  27. {% endfor %}
  28. Opts
  29. ====
  30. The `opts` variable abstracts the contents of the minion's configuration file
  31. directly to the template. The `opts` variable is a dictionary. It is available
  32. in all templates.
  33. .. code-block:: jinja
  34. {{ opts['cachedir'] }}
  35. The ``config.get`` function also searches for values in the `opts` dictionary.
  36. Pillar
  37. ======
  38. The `pillar` dictionary can be referenced directly, and is available in all
  39. templates:
  40. .. code-block:: jinja
  41. {{ pillar['key'] }}
  42. Using the ``pillar.get`` function via the `salt` variable is generally
  43. recommended since a default can be safely set in the event that the value
  44. is not available in pillar and dictionaries can be traversed directly:
  45. .. code-block:: jinja
  46. {{ salt['pillar.get']('key', 'failover_value') }}
  47. {{ salt['pillar.get']('stuff:more:deeper') }}
  48. Grains
  49. ======
  50. The `grains` dictionary makes the minion's grains directly available, and is
  51. available in all templates:
  52. .. code-block:: jinja
  53. {{ grains['os'] }}
  54. The ``grains.get`` function can be used to traverse deeper grains and set
  55. defaults:
  56. .. code-block:: jinja
  57. {{ salt['grains.get']('os') }}
  58. saltenv
  59. =======
  60. The `saltenv` variable is available in only in sls files when gathering the sls
  61. from an environment.
  62. .. code-block:: jinja
  63. {{ saltenv }}
  64. SLS Only Variables
  65. ==================
  66. The following are only available when processing sls files. If you need these
  67. in other templates, you can usually pass them in as template context.
  68. sls
  69. ---
  70. The `sls` variable contains the sls reference value, and is only available in
  71. the actual SLS file (not in any files referenced in that SLS). The sls
  72. reference value is the value used to include the sls in top files or via the
  73. include option.
  74. .. code-block:: jinja
  75. {{ sls }}
  76. slspath
  77. -------
  78. The `slspath` variable contains the path to the directory of the current sls
  79. file. The value of `slspath` in files referenced in the current sls depends on
  80. the reference method. For jinja includes `slspath` is the path to the current
  81. directory of the file. For salt includes `slspath` is the path to the directory
  82. of the included file. If current sls file is in root of the file roots, this
  83. will return ""
  84. .. code-block:: jinja
  85. {{ slspath }}
  86. sls_path
  87. --------
  88. A version of `slspath` with underscores as path separators instead of slashes.
  89. So, if `slspath` is `path/to/state` then `sls_path` is `path_to_state`
  90. .. code-block:: jinja
  91. {{ sls_path }}
  92. slsdotpath
  93. ----------
  94. A version of `slspath` with dots as path separators instead of slashes. So, if
  95. `slspath` is `path/to/state` then `slsdotpath` is `path.to.state`. This is same
  96. as `sls` if `sls` points to a directory instead if a file.
  97. .. code-block:: jinja
  98. {{ slsdotpath }}
  99. slscolonpath
  100. ------------
  101. A version of `slspath` with colons (`:`) as path separators instead of slashes.
  102. So, if `slspath` is `path/to/state` then `slscolonpath` is `path:to:state`.
  103. .. code-block:: jinja
  104. {{ slscolonpath }}
  105. tplpath
  106. -------
  107. Full path to sls template file being process on local disk. This is usually
  108. pointing to a copy of the sls file in a cache directory. This will be in OS
  109. specific format (Windows vs POSIX). (It is probably best not to use this.)
  110. .. code-block:: jinja
  111. {{ tplpath }}
  112. tplfile
  113. -------
  114. Relative path to exact sls template file being processed relative to file
  115. roots.
  116. .. code-block:: jinja
  117. {{ tplfile }}
  118. tpldir
  119. ------
  120. Directory, relative to file roots, of the current sls file. If current sls file
  121. is in root of the file roots, this will return ".". This is usually identical
  122. to `slspath` except in case of root-level sls, where this will return a "`.`".
  123. A Common use case for this variable is to generate relative salt urls like:
  124. .. code-block:: jinja
  125. my-file:
  126. file.managed:
  127. source: salt://{{ tpldir }}/files/my-template
  128. tpldot
  129. ------
  130. A version of `tpldir` with dots as path separators instead of slashes. So, if
  131. `tpldir` is `path/to/state` then `tpldot` is `path.to.state`. NOTE: if `tpldir`
  132. is `.`, this will be set to ""
  133. .. code-block:: jinja
  134. {{ tpldot }}