highstate.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. .. _states-highstate:
  2. ====================================
  3. Highstate data structure definitions
  4. ====================================
  5. The Salt State Tree
  6. ===================
  7. A state tree is a collection of ``SLS`` files and directories that live under the directory
  8. specified in :conf_master:`file_roots`.
  9. .. note::
  10. Directory names or filenames in the state tree cannot contain a period, with the
  11. exception of the period in the .sls file suffix.
  12. .. _states-highstate-top-file:
  13. Top file
  14. --------
  15. The main state file that instructs minions what environment and modules to use
  16. during state execution.
  17. Configurable via :conf_master:`state_top`.
  18. .. seealso:: :ref:`A detailed description of the top file <states-top>`
  19. .. _include-declaration:
  20. Include declaration
  21. -------------------
  22. Defines a list of :ref:`module-reference` strings to include in this ``SLS``.
  23. Occurs only in the top level of the SLS data structure.
  24. Example:
  25. .. code-block:: yaml
  26. include:
  27. - edit.vim
  28. - http.server
  29. .. _module-reference:
  30. Module reference
  31. ----------------
  32. The name of a SLS module defined by a separate SLS file and residing on
  33. the Salt Master. A module named ``edit.vim`` is a reference to the SLS
  34. file ``salt://edit/vim.sls``.
  35. .. _id-declaration:
  36. ID declaration
  37. --------------
  38. Defines an individual :ref:`highstate <running-highstate>` component. Always
  39. references a value of a dictionary containing keys referencing
  40. :ref:`state-declaration` and :ref:`requisite-declaration`. Can be overridden by
  41. a :ref:`name-declaration` or a :ref:`names-declaration`.
  42. Occurs on the top level or under the :ref:`extend-declaration`.
  43. Must be unique across entire state tree. If the same ID declaration is
  44. used twice, only the first one matched will be used. All subsequent
  45. ID declarations with the same name will be ignored.
  46. .. note:: Naming gotchas
  47. In Salt versions earlier than 0.9.7, ID declarations containing dots would
  48. result in unpredictable output.
  49. .. _extend-declaration:
  50. Extend declaration
  51. ------------------
  52. Extends a :ref:`name-declaration` from an included ``SLS module``. The
  53. keys of the extend declaration always refer to an existing
  54. :ref:`id-declaration` which have been defined in included ``SLS modules``.
  55. Occurs only in the top level and defines a dictionary.
  56. States cannot be extended more than once in a single state run.
  57. Extend declarations are useful for adding-to or overriding parts of a
  58. :ref:`state-declaration` that is defined in another ``SLS`` file. In the
  59. following contrived example, the shown ``mywebsite.sls`` file is ``include``
  60. -ing and ``extend`` -ing the ``apache.sls`` module in order to add a ``watch``
  61. declaration that will restart Apache whenever the Apache configuration file,
  62. ``mywebsite`` changes.
  63. .. code-block:: yaml
  64. include:
  65. - apache
  66. extend:
  67. apache:
  68. service:
  69. - watch:
  70. - file: mywebsite
  71. mywebsite:
  72. file.managed:
  73. - name: /var/www/mysite
  74. .. seealso:: watch_in and require_in
  75. Sometimes it is more convenient to use the :ref:`watch_in
  76. <requisites-watch-in>` or :ref:`require_in <requisites-require-in>` syntax
  77. instead of extending another ``SLS`` file.
  78. :ref:`State Requisites <requisites>`
  79. .. _state-declaration:
  80. State declaration
  81. -----------------
  82. A list which contains one string defining the :ref:`function-declaration` and
  83. any number of :ref:`function-arg-declaration` dictionaries.
  84. Can, optionally, contain a number of additional components like the
  85. name override components — :ref:`name <name-declaration>` and
  86. :ref:`names <names-declaration>`. Can also contain :ref:`requisite
  87. declarations <requisite-declaration>`.
  88. Occurs under an :ref:`ID-declaration`.
  89. .. _requisite-declaration:
  90. Requisite declaration
  91. ---------------------
  92. A list containing :ref:`requisite references <requisite-reference>`.
  93. Used to build the action dependency tree. While Salt states are made to
  94. execute in a deterministic order, this order is managed by requiring
  95. and watching other Salt states.
  96. Occurs as a list component under a :ref:`state-declaration` or as a
  97. key under an :ref:`ID-declaration`.
  98. .. _requisite-reference:
  99. Requisite reference
  100. -------------------
  101. A single key dictionary. The key is the name of the referenced
  102. :ref:`state-declaration` and the value is the ID of the referenced
  103. :ref:`ID-declaration`.
  104. Occurs as a single index in a :ref:`requisite-declaration` list.
  105. .. _function-declaration:
  106. Function declaration
  107. --------------------
  108. The name of the function to call within the state. A state declaration
  109. can contain only a single function declaration.
  110. For example, the following state declaration calls the :mod:`installed
  111. <salt.states.pkg.installed>` function in the ``pkg`` state module:
  112. .. code-block:: yaml
  113. httpd:
  114. pkg.installed: []
  115. The function can be declared inline with the state as a shortcut.
  116. The actual data structure is compiled to this form:
  117. .. code-block:: yaml
  118. httpd:
  119. pkg:
  120. - installed
  121. Where the function is a string in the body of the state declaration.
  122. Technically when the function is declared in dot notation the compiler
  123. converts it to be a string in the state declaration list. Note that the
  124. use of the first example more than once in an ID declaration is invalid
  125. yaml.
  126. INVALID:
  127. .. code-block:: yaml
  128. httpd:
  129. pkg.installed
  130. service.running
  131. When passing a function without arguments and another state declaration
  132. within a single ID declaration, then the long or "standard" format
  133. needs to be used since otherwise it does not represent a valid data
  134. structure.
  135. VALID:
  136. .. code-block:: yaml
  137. httpd:
  138. pkg.installed: []
  139. service.running: []
  140. Occurs as the only index in the :ref:`state-declaration` list.
  141. .. _function-arg-declaration:
  142. Function arg declaration
  143. ------------------------
  144. A single key dictionary referencing a Python type which is to be passed
  145. to the named :ref:`function-declaration` as a parameter. The type must
  146. be the data type expected by the function.
  147. Occurs under a :ref:`function-declaration`.
  148. For example in the following state declaration ``user``, ``group``, and
  149. ``mode`` are passed as arguments to the :mod:`managed
  150. <salt.states.file.managed>` function in the ``file`` state module:
  151. .. code-block:: yaml
  152. /etc/http/conf/http.conf:
  153. file.managed:
  154. - user: root
  155. - group: root
  156. - mode: 644
  157. .. _name-declaration:
  158. Name declaration
  159. ----------------
  160. Overrides the ``name`` argument of a :ref:`state-declaration`. If
  161. ``name`` is not specified the :ref:`ID-declaration` satisfies the
  162. ``name`` argument.
  163. The name is always a single key dictionary referencing a string.
  164. Overriding ``name`` is useful for a variety of scenarios.
  165. For example, avoiding clashing ID declarations. The following two state
  166. declarations cannot both have ``/etc/motd`` as the ID declaration:
  167. .. code-block:: yaml
  168. motd_perms:
  169. file.managed:
  170. - name: /etc/motd
  171. - mode: 644
  172. motd_quote:
  173. file.append:
  174. - name: /etc/motd
  175. - text: "Of all smells, bread; of all tastes, salt."
  176. Another common reason to override ``name`` is if the ID declaration is long and
  177. needs to be referenced in multiple places. In the example below it is much
  178. easier to specify ``mywebsite`` than to specify
  179. ``/etc/apache2/sites-available/mywebsite.com`` multiple times:
  180. .. code-block:: yaml
  181. mywebsite:
  182. file.managed:
  183. - name: /etc/apache2/sites-available/mywebsite.com
  184. - source: salt://mywebsite.com
  185. a2ensite mywebsite.com:
  186. cmd.wait:
  187. - unless: test -L /etc/apache2/sites-enabled/mywebsite.com
  188. - watch:
  189. - file: mywebsite
  190. apache2:
  191. service.running:
  192. - watch:
  193. - file: mywebsite
  194. .. _names-declaration:
  195. Names declaration
  196. -----------------
  197. Expands the contents of the containing :ref:`state-declaration` into
  198. multiple state declarations, each with its own name.
  199. For example, given the following state declaration:
  200. .. code-block:: yaml
  201. python-pkgs:
  202. pkg.installed:
  203. - names:
  204. - python-django
  205. - python-crypto
  206. - python-yaml
  207. Once converted into the lowstate data structure the above state
  208. declaration will be expanded into the following three state declarations:
  209. .. code-block:: yaml
  210. python-django:
  211. pkg.installed
  212. python-crypto:
  213. pkg.installed
  214. python-yaml:
  215. pkg.installed
  216. Other values can be overridden during the expansion by providing an additional
  217. dictionary level.
  218. .. versionadded:: 2014.7.0
  219. .. code-block:: yaml
  220. ius:
  221. pkgrepo.managed:
  222. - humanname: IUS Community Packages for Enterprise Linux 6 - $basearch
  223. - gpgcheck: 1
  224. - baseurl: http://mirror.rackspace.com/ius/stable/CentOS/6/$basearch
  225. - gpgkey: http://dl.iuscommunity.org/pub/ius/IUS-COMMUNITY-GPG-KEY
  226. - names:
  227. - ius
  228. - ius-devel:
  229. - baseurl: http://mirror.rackspace.com/ius/development/CentOS/6/$basearch
  230. .. _states-highstate-example:
  231. Large example
  232. =============
  233. Here is the layout in yaml using the names of the highdata structure
  234. components.
  235. .. code-block:: yaml
  236. <Include Declaration>:
  237. - <Module Reference>
  238. - <Module Reference>
  239. <Extend Declaration>:
  240. <ID Declaration>:
  241. [<overrides>]
  242. # standard declaration
  243. <ID Declaration>:
  244. <State Module>:
  245. - <Function>
  246. - <Function Arg>
  247. - <Function Arg>
  248. - <Function Arg>
  249. - <Name>: <name>
  250. - <Requisite Declaration>:
  251. - <Requisite Reference>
  252. - <Requisite Reference>
  253. # inline function and names
  254. <ID Declaration>:
  255. <State Module>.<Function>:
  256. - <Function Arg>
  257. - <Function Arg>
  258. - <Function Arg>
  259. - <Names>:
  260. - <name>
  261. - <name>
  262. - <name>
  263. - <Requisite Declaration>:
  264. - <Requisite Reference>
  265. - <Requisite Reference>
  266. # multiple states for single id
  267. <ID Declaration>:
  268. <State Module>:
  269. - <Function>
  270. - <Function Arg>
  271. - <Name>: <name>
  272. - <Requisite Declaration>:
  273. - <Requisite Reference>
  274. <State Module>:
  275. - <Function>
  276. - <Function Arg>
  277. - <Names>:
  278. - <name>
  279. - <name>
  280. - <Requisite Declaration>:
  281. - <Requisite Reference>