compiler_ordering.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. .. _compiler-ordering:
  2. =====================================
  3. Understanding State Compiler Ordering
  4. =====================================
  5. .. note::
  6. This tutorial is an intermediate level tutorial. Some basic understanding
  7. of the state system and writing Salt Formulas is assumed.
  8. Salt's state system is built to deliver all of the power of configuration
  9. management systems without sacrificing simplicity. This tutorial is made to
  10. help users understand in detail just how the order is defined for state
  11. executions in Salt.
  12. This tutorial is written to represent the behavior of Salt as of version
  13. 0.17.0.
  14. Compiler Basics
  15. ===============
  16. To understand ordering in depth some very basic knowledge about the state
  17. compiler is very helpful. No need to worry though, this is very high level!
  18. High Data and Low Data
  19. ----------------------
  20. When defining Salt Formulas in YAML the data that is being represented is
  21. referred to by the compiler as High Data. When the data is initially
  22. loaded into the compiler it is a single large python dictionary, this
  23. dictionary can be viewed raw by running:
  24. .. code-block:: bash
  25. salt '*' state.show_highstate
  26. This "High Data" structure is then compiled down to "Low Data". The Low
  27. Data is what is matched up to create individual executions in Salt's
  28. configuration management system. The
  29. low data is an ordered list of single state calls to execute. Once the
  30. low data is compiled the evaluation order can be seen.
  31. The low data can be viewed by running:
  32. .. code-block:: bash
  33. salt '*' state.show_lowstate
  34. .. note::
  35. The state execution module contains MANY functions for evaluating the
  36. state system and is well worth a read! These routines can be very useful
  37. when debugging states or to help deepen one's understanding of Salt's
  38. state system.
  39. As an example, a state written thusly:
  40. .. code-block:: yaml
  41. apache:
  42. pkg.installed:
  43. - name: httpd
  44. service.running:
  45. - name: httpd
  46. - watch:
  47. - file: apache_conf
  48. - pkg: apache
  49. apache_conf:
  50. file.managed:
  51. - name: /etc/httpd/conf.d/httpd.conf
  52. - source: salt://apache/httpd.conf
  53. Will have High Data which looks like this represented in json:
  54. .. code-block:: json
  55. {
  56. "apache": {
  57. "pkg": [
  58. {
  59. "name": "httpd"
  60. },
  61. "installed",
  62. {
  63. "order": 10000
  64. }
  65. ],
  66. "service": [
  67. {
  68. "name": "httpd"
  69. },
  70. {
  71. "watch": [
  72. {
  73. "file": "apache_conf"
  74. },
  75. {
  76. "pkg": "apache"
  77. }
  78. ]
  79. },
  80. "running",
  81. {
  82. "order": 10001
  83. }
  84. ],
  85. "__sls__": "blah",
  86. "__env__": "base"
  87. },
  88. "apache_conf": {
  89. "file": [
  90. {
  91. "name": "/etc/httpd/conf.d/httpd.conf"
  92. },
  93. {
  94. "source": "salt://apache/httpd.conf"
  95. },
  96. "managed",
  97. {
  98. "order": 10002
  99. }
  100. ],
  101. "__sls__": "blah",
  102. "__env__": "base"
  103. }
  104. }
  105. The subsequent Low Data will look like this:
  106. .. code-block:: json
  107. [
  108. {
  109. "name": "httpd",
  110. "state": "pkg",
  111. "__id__": "apache",
  112. "fun": "installed",
  113. "__env__": "base",
  114. "__sls__": "blah",
  115. "order": 10000
  116. },
  117. {
  118. "name": "httpd",
  119. "watch": [
  120. {
  121. "file": "apache_conf"
  122. },
  123. {
  124. "pkg": "apache"
  125. }
  126. ],
  127. "state": "service",
  128. "__id__": "apache",
  129. "fun": "running",
  130. "__env__": "base",
  131. "__sls__": "blah",
  132. "order": 10001
  133. },
  134. {
  135. "name": "/etc/httpd/conf.d/httpd.conf",
  136. "source": "salt://apache/httpd.conf",
  137. "state": "file",
  138. "__id__": "apache_conf",
  139. "fun": "managed",
  140. "__env__": "base",
  141. "__sls__": "blah",
  142. "order": 10002
  143. }
  144. ]
  145. This tutorial discusses the Low Data evaluation and the state runtime.
  146. Ordering Layers
  147. ===============
  148. Salt defines 2 order interfaces which are evaluated in the state runtime and
  149. defines these orders in a number of passes.
  150. Definition Order
  151. ----------------
  152. .. note::
  153. The Definition Order system can be disabled by turning the option
  154. ``state_auto_order`` to ``False`` in the master configuration file.
  155. The top level of ordering is the `Definition Order`. The `Definition Order`
  156. is the order in which states are defined in salt formulas. This is very
  157. straightforward on basic states which do not contain ``include`` statements
  158. or a ``top`` file, as the states are just ordered from the top of the file,
  159. but the include system starts to bring in some simple rules for how the
  160. `Definition Order` is defined.
  161. Looking back at the "Low Data" and "High Data" shown above, the order key has
  162. been transparently added to the data to enable the `Definition Order`.
  163. The Include Statement
  164. ~~~~~~~~~~~~~~~~~~~~~
  165. Basically, if there is an include statement in a formula, then the formulas
  166. which are included will be run BEFORE the contents of the formula which
  167. is including them. Also, the include statement is a list, so they will be
  168. loaded in the order in which they are included.
  169. In the following case:
  170. ``foo.sls``
  171. .. code-block:: yaml
  172. include:
  173. - bar
  174. - baz
  175. ``bar.sls``
  176. .. code-block:: yaml
  177. include:
  178. - quo
  179. ``baz.sls``
  180. .. code-block:: yaml
  181. include:
  182. - qux
  183. In the above case if ``state.apply foo`` were called then the formulas will be
  184. loaded in the following order:
  185. 1. quo
  186. 2. bar
  187. 3. qux
  188. 4. baz
  189. 5. foo
  190. The `order` Flag
  191. ----------------
  192. The `Definition Order` happens transparently in the background, but the
  193. ordering can be explicitly overridden using the ``order`` flag in states:
  194. .. code-block:: yaml
  195. apache:
  196. pkg.installed:
  197. - name: httpd
  198. - order: 1
  199. This order flag will over ride the definition order, this makes it very
  200. simple to create states that are always executed first, last or in specific
  201. stages, a great example is defining a number of package repositories that
  202. need to be set up before anything else, or final checks that need to be
  203. run at the end of a state run by using ``order: last`` or ``order: -1``.
  204. When the order flag is explicitly set the `Definition Order` system will omit
  205. setting an order for that state and directly use the order flag defined.
  206. Lexicographical Fall-back
  207. -------------------------
  208. Salt states were written to ALWAYS execute in the same order. Before the
  209. introduction of `Definition Order` in version 0.17.0 everything was ordered
  210. lexicographically according to the name of the state, then function then id.
  211. This is the way Salt has always ensured that states always run in the same
  212. order regardless of where they are deployed, the addition of the
  213. `Definition Order` method mealy makes this finite ordering easier to follow.
  214. The lexicographical ordering is still applied but it only has any effect when
  215. two order statements collide. This means that if multiple states are assigned
  216. the same order number that they will fall back to lexicographical ordering
  217. to ensure that every execution still happens in a finite order.
  218. .. note::
  219. If running with ``state_auto_order: False`` the ``order`` key is not
  220. set automatically, since the Lexicographical order can be derived
  221. from other keys.
  222. Requisite Ordering
  223. ------------------
  224. Salt states are fully declarative, in that they are written to declare the
  225. state in which a system should be. This means that components can require that
  226. other components have been set up successfully. Unlike the other ordering
  227. systems, the `Requisite` system in Salt is evaluated at runtime.
  228. The requisite system is also built to ensure that the ordering of execution
  229. never changes, but is always the same for a given set of states. This is
  230. accomplished by using a runtime that processes states in a completely
  231. predictable order instead of using an event loop based system like other
  232. declarative configuration management systems.
  233. Runtime Requisite Evaluation
  234. ----------------------------
  235. The requisite system is evaluated as the components are found, and the
  236. requisites are always evaluated in the same order. This explanation will
  237. be followed by an example, as the raw explanation may be a little dizzying
  238. at first as it creates a linear dependency evaluation sequence.
  239. The "Low Data" is an ordered list or dictionaries, the state runtime evaluates
  240. each dictionary in the order in which they are arranged in the list. When
  241. evaluating a single dictionary it is checked for requisites, requisites are
  242. evaluated in order, ``require`` then ``watch`` then ``prereq``.
  243. .. note::
  244. If using requisite in statements like require_in and watch_in these will
  245. be compiled down to require and watch statements before runtime evaluation.
  246. Each requisite contains an ordered list of requisites, these requisites are
  247. looked up in the list of dictionaries and then executed. Once all requisites
  248. have been evaluated and executed then the requiring state can safely be run
  249. (or not run if requisites have not been met).
  250. This means that the requisites are always evaluated in the same order, again
  251. ensuring one of the core design principals of Salt's State system to ensure
  252. that execution is always finite is intact.
  253. Simple Runtime Evaluation Example
  254. ---------------------------------
  255. Given the above "Low Data" the states will be evaluated in the following order:
  256. 1. The pkg.installed is executed ensuring that the apache package is
  257. installed, it contains no requisites and is therefore the first defined
  258. state to execute.
  259. 2. The service.running state is evaluated but NOT executed, a watch requisite
  260. is found, therefore they are read in order, the runtime first checks for
  261. the file, sees that it has not been executed and calls for the file state
  262. to be evaluated.
  263. 3. The file state is evaluated AND executed, since it, like the pkg state does
  264. not contain any requisites.
  265. 4. The evaluation of the service state continues, it next checks the pkg
  266. requisite and sees that it is met, with all requisites met the service
  267. state is now executed.
  268. Best Practice
  269. -------------
  270. The best practice in Salt is to choose a method and stick with it, official
  271. states are written using requisites for all associations since requisites
  272. create clean, traceable dependency trails and make for the most portable
  273. formulas. To accomplish something similar to how classical imperative
  274. systems function all requisites can be omitted and the ``failhard`` option
  275. then set to ``True`` in the master configuration, this will stop all state runs at
  276. the first instance of a failure.
  277. In the end, using requisites creates very tight and fine grained states,
  278. not using requisites makes full sequence runs and while slightly easier
  279. to write, and gives much less control over the executions.