ordering.rst 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. .. _ordering:
  2. ===============
  3. Ordering States
  4. ===============
  5. The way in which configuration management systems are executed is a hotly
  6. debated topic in the configuration management world. Two major philosophies
  7. exist on the subject, to either execute in an imperative fashion where things
  8. are executed in the order in which they are defined, or in a declarative
  9. fashion where dependencies need to be mapped between objects.
  10. Imperative ordering is finite and generally considered easier to write, but
  11. declarative ordering is much more powerful and flexible but generally considered
  12. more difficult to create.
  13. Salt has been created to get the best of both worlds. States are evaluated in
  14. a finite order, which guarantees that states are always executed in the same
  15. order, and the states runtime is declarative, making Salt fully aware of
  16. dependencies via the `requisite` system.
  17. .. _ordering_auto_order:
  18. State Auto Ordering
  19. ===================
  20. .. versionadded: 0.17.0
  21. Salt always executes states in a finite manner, meaning that they will always
  22. execute in the same order regardless of the system that is executing them. This
  23. evaluation order makes it easy to know what order the states will be executed in,
  24. but it is important to note that the requisite system will override the ordering
  25. defined in the files, and the ``order`` option, described below, will also
  26. override the order in which states are executed.
  27. This ordering system can be disabled in preference of lexicographic (classic)
  28. ordering by setting the ``state_auto_order`` option to ``False`` in the master
  29. configuration file. Otherwise, ``state_auto_order`` defaults to ``True``.
  30. How compiler ordering is managed is described further in :ref:`compiler-ordering`.
  31. .. _ordering_requisites:
  32. Requisite Statements
  33. ====================
  34. .. note::
  35. The behavior of requisites changed in version 0.9.7 of Salt. This
  36. documentation applies to requisites in version 0.9.7 and later.
  37. Often when setting up states any single action will require or depend on
  38. another action. Salt allows for the building of relationships between states
  39. with requisite statements. A requisite statement ensures that the named state
  40. is evaluated before the state requiring it. There are three types of requisite
  41. statements in Salt, **require**, **watch**, and **prereq**.
  42. These requisite statements are applied to a specific state declaration:
  43. .. code-block:: yaml
  44. httpd:
  45. pkg.installed: []
  46. file.managed:
  47. - name: /etc/httpd/conf/httpd.conf
  48. - source: salt://httpd/httpd.conf
  49. - require:
  50. - pkg: httpd
  51. In this example, the **require** requisite is used to declare that the file
  52. /etc/httpd/conf/httpd.conf should only be set up if the pkg state executes
  53. successfully.
  54. The requisite system works by finding the states that are required and
  55. executing them before the state that requires them. Then the required states
  56. can be evaluated to see if they have executed correctly.
  57. Require statements can refer to any state defined in Salt. The basic examples
  58. are `pkg`, `service`, and `file`, but any used state can be referenced.
  59. In addition to state declarations such as pkg, file, etc., **sls** type requisites
  60. are also recognized, and essentially allow 'chaining' of states. This provides a
  61. mechanism to ensure the proper sequence for complex state formulas, especially when
  62. the discrete states are split or groups into separate sls files:
  63. .. code-block:: yaml
  64. include:
  65. - network
  66. httpd:
  67. pkg.installed: []
  68. service.running:
  69. - require:
  70. - pkg: httpd
  71. - sls: network
  72. In this example, the httpd service running state will not be applied
  73. (i.e., the httpd service will not be started) unless both the httpd package is
  74. installed AND the network state is satisfied.
  75. .. note:: Requisite matching
  76. Requisites match on both the ID Declaration and the ``name`` parameter.
  77. Therefore, if using the ``pkgs`` or ``sources`` argument to install
  78. a list of packages in a pkg state, it's important to note that it is
  79. impossible to match an individual package in the list, since all packages
  80. are installed as a single state.
  81. Multiple Requisites
  82. -------------------
  83. The requisite statement is passed as a list, allowing for the easy addition of
  84. more requisites. Both requisite types can also be separately declared:
  85. .. code-block:: yaml
  86. httpd:
  87. pkg.installed: []
  88. service.running:
  89. - enable: True
  90. - watch:
  91. - file: /etc/httpd/conf/httpd.conf
  92. - require:
  93. - pkg: httpd
  94. - user: httpd
  95. - group: httpd
  96. file.managed:
  97. - name: /etc/httpd/conf/httpd.conf
  98. - source: salt://httpd/httpd.conf
  99. - require:
  100. - pkg: httpd
  101. user.present: []
  102. group.present: []
  103. In this example, the httpd service is only going to be started if the package,
  104. user, group, and file are executed successfully.
  105. Requisite Documentation
  106. -----------------------
  107. For detailed information on each of the individual requisites, :ref:`please
  108. look here. <requisites>`
  109. The Order Option
  110. ================
  111. Before using the `order` option, remember that the majority of state ordering
  112. should be done with a :ref:`requisite-declaration`, and that a requisite
  113. declaration will override an `order` option, so a state with order option
  114. should not require or required by other states.
  115. The order option is used by adding an order number to a state declaration
  116. with the option `order`:
  117. .. code-block:: yaml
  118. vim:
  119. pkg.installed:
  120. - order: 1
  121. By adding the order option to `1` this ensures that the vim package will be
  122. installed in tandem with any other state declaration set to the order `1`.
  123. Any state declared without an order option will be executed after all states
  124. with order options are executed.
  125. But this construct can only handle ordering states from the beginning.
  126. Certain circumstances will present a situation where it is desirable to send
  127. a state to the end of the line. To do this, set the order to ``last``:
  128. .. code-block:: yaml
  129. vim:
  130. pkg.installed:
  131. - order: last