ordering.rst 6.1 KB

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