index.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. .. _state-system-reference:
  2. ======================
  3. State System Reference
  4. ======================
  5. Salt offers an interface to manage the configuration or "state" of the
  6. Salt minions. This interface is a fully capable mechanism used to enforce the
  7. state of systems from a central manager.
  8. .. toctree::
  9. :glob:
  10. *
  11. State Management
  12. ================
  13. State management, also frequently called Software Configuration Management
  14. (SCM), is a program that puts and keeps a system into a predetermined state. It
  15. installs software packages, starts or restarts services or puts configuration
  16. files in place and watches them for changes.
  17. Having a state management system in place allows one to easily and reliably
  18. configure and manage a few servers or a few thousand servers. It allows
  19. configurations to be kept under version control.
  20. Salt States is an extension of the Salt Modules that we discussed in the
  21. previous :ref:`remote execution <tutorial-remote-execution-modules>` tutorial. Instead
  22. of calling one-off executions the state of a system can be easily defined and
  23. then enforced.
  24. Understanding the Salt State System Components
  25. ==============================================
  26. The Salt state system is comprised of a number of components. As a user, an
  27. understanding of the SLS and renderer systems are needed. But as a developer,
  28. an understanding of Salt states and how to write the states is needed as well.
  29. .. note::
  30. States are compiled and executed only on minions that have been targeted.
  31. To execute functions directly on masters, see :ref:`runners <runners>`.
  32. Salt SLS System
  33. ---------------
  34. The primary system used by the Salt state system is the SLS system. SLS stands
  35. for **S**\ a\ **L**\ t **S**\ tate.
  36. The Salt States are files which contain the information about how to configure
  37. Salt minions. The states are laid out in a directory tree and can be written in
  38. many different formats.
  39. The contents of the files and the way they are laid out is intended to be as
  40. simple as possible while allowing for maximum flexibility. The files are laid
  41. out in states and contains information about how the minion needs to be
  42. configured.
  43. SLS File Layout
  44. ```````````````
  45. SLS files are laid out in the Salt file server.
  46. A simple layout can look like this:
  47. .. code-block:: yaml
  48. top.sls
  49. ssh.sls
  50. sshd_config
  51. users/init.sls
  52. users/admin.sls
  53. salt/master.sls
  54. web/init.sls
  55. The ``top.sls`` file is a key component. The ``top.sls`` files
  56. is used to determine which SLS files should be applied to which minions.
  57. The rest of the files with the ``.sls`` extension in the above example are
  58. state files.
  59. Files without a ``.sls`` extensions are seen by the Salt master as
  60. files that can be downloaded to a Salt minion.
  61. States are translated into dot notation. For example, the ``ssh.sls`` file is
  62. seen as the ssh state and the ``users/admin.sls`` file is seen as the
  63. users.admin state.
  64. Files named ``init.sls`` are translated to be the state name of the parent
  65. directory, so the ``web/init.sls`` file translates to the ``web`` state.
  66. In Salt, everything is a file; there is no "magic translation" of files and file
  67. types. This means that a state file can be distributed to minions just like a
  68. plain text or binary file.
  69. SLS Files
  70. `````````
  71. The Salt state files are simple sets of data. Since SLS files are just data
  72. they can be represented in a number of different ways.
  73. The default format is YAML generated from a Jinja template. This allows for the
  74. states files to have all the language constructs of Python and the simplicity of YAML.
  75. State files can then be complicated Jinja templates that translate down to YAML, or just
  76. plain and simple YAML files.
  77. The State files are simply common data structures such as dictionaries and lists, constructed
  78. using a templating language such as YAML.
  79. Here is an example of a Salt State:
  80. .. code-block:: yaml
  81. vim:
  82. pkg.installed: []
  83. salt:
  84. pkg.latest:
  85. - name: salt
  86. service.running:
  87. - names:
  88. - salt-master
  89. - salt-minion
  90. - require:
  91. - pkg: salt
  92. - watch:
  93. - file: /etc/salt/minion
  94. /etc/salt/minion:
  95. file.managed:
  96. - source: salt://salt/minion
  97. - user: root
  98. - group: root
  99. - mode: 644
  100. - require:
  101. - pkg: salt
  102. This short stanza will ensure that vim is installed, Salt is installed and up
  103. to date, the salt-master and salt-minion daemons are running and the Salt
  104. minion configuration file is in place. It will also ensure everything is
  105. deployed in the right order and that the Salt services are restarted when the
  106. watched file updated.
  107. The Top File
  108. ````````````
  109. The top file controls the mapping between minions and the states which should
  110. be applied to them.
  111. The top file specifies which minions should have which SLS files applied and
  112. which environments they should draw those SLS files from.
  113. The top file works by specifying environments on the top-level.
  114. Each environment contains :ref:`target expressions <targeting>` to match
  115. minions. Finally, each target expression contains a list of Salt states to
  116. apply to matching minions:
  117. .. code-block:: yaml
  118. base:
  119. '*':
  120. - salt
  121. - users
  122. - users.admin
  123. 'saltmaster.*':
  124. - match: pcre
  125. - salt.master
  126. This above example uses the base environment which is built into the default
  127. Salt setup.
  128. The base environment has target expressions. The first one matches all minions,
  129. and the SLS files below it apply to all minions.
  130. The second expression is a regular expression that will match all minions
  131. with an ID matching ``saltmaster.*`` and specifies that for those minions, the
  132. salt.master state should be applied.
  133. .. important::
  134. Since version 2014.7.0, the default matcher (when one is not explicitly
  135. defined as in the second expression in the above example) is the
  136. :ref:`compound <targeting-compound>` matcher. Since this matcher parses
  137. individual words in the expression, minion IDs containing spaces will not
  138. match properly using this matcher. Therefore, if your target expression is
  139. designed to match a minion ID containing spaces, it will be necessary to
  140. specify a different match type (such as ``glob``). For example:
  141. .. code-block:: yaml
  142. base:
  143. 'test minion':
  144. - match: glob
  145. - foo
  146. - bar
  147. - baz
  148. A full table of match types available in the top file can be found :ref:`here
  149. <top-file-match-types>`.
  150. .. _reloading-modules:
  151. Reloading Modules
  152. -----------------
  153. Some Salt states require that specific packages be installed in order for the
  154. module to load. As an example the :mod:`pip <salt.states.pip_state>` state
  155. module requires the `pip`_ package for proper name and version parsing.
  156. In most of the common cases, Salt is clever enough to transparently reload the
  157. modules. For example, if you install a package, Salt reloads modules because
  158. some other module or state might require just that package which was installed.
  159. On some edge-cases salt might need to be told to reload the modules. Consider
  160. the following state file which we'll call ``pep8.sls``:
  161. .. code-block:: yaml
  162. python-pip:
  163. cmd.run:
  164. - name: |
  165. easy_install --script-dir=/usr/bin -U pip
  166. - cwd: /
  167. pep8:
  168. pip.installed:
  169. - require:
  170. - cmd: python-pip
  171. The above example installs `pip`_ using ``easy_install`` from `setuptools`_ and
  172. installs `pep8`_ using :mod:`pip <salt.states.pip_state>`, which, as told
  173. earlier, requires `pip`_ to be installed system-wide. Let's execute this state:
  174. .. code-block:: bash
  175. salt-call state.apply pep8
  176. The execution output would be something like:
  177. .. code-block:: text
  178. ----------
  179. State: - pip
  180. Name: pep8
  181. Function: installed
  182. Result: False
  183. Comment: State pip.installed found in sls pep8 is unavailable
  184. Changes:
  185. Summary
  186. ------------
  187. Succeeded: 1
  188. Failed: 1
  189. ------------
  190. Total: 2
  191. If we executed the state again the output would be:
  192. .. code-block:: text
  193. ----------
  194. State: - pip
  195. Name: pep8
  196. Function: installed
  197. Result: True
  198. Comment: Package was successfully installed
  199. Changes: pep8==1.4.6: Installed
  200. Summary
  201. ------------
  202. Succeeded: 2
  203. Failed: 0
  204. ------------
  205. Total: 2
  206. Since we installed `pip`_ using :mod:`cmd <salt.states.cmd>`, Salt has no way
  207. to know that a system-wide package was installed.
  208. On the second execution, since the required `pip`_ package was installed, the
  209. state executed correctly.
  210. .. note::
  211. Salt does not reload modules on every state run because doing so would greatly
  212. slow down state execution.
  213. So how do we solve this *edge-case*? ``reload_modules``!
  214. ``reload_modules`` is a boolean option recognized by salt on **all** available
  215. states which forces salt to reload its modules once a given state finishes.
  216. The modified state file would now be:
  217. .. code-block:: yaml
  218. python-pip:
  219. cmd.run:
  220. - name: |
  221. easy_install --script-dir=/usr/bin -U pip
  222. - cwd: /
  223. - reload_modules: true
  224. pep8:
  225. pip.installed:
  226. - require:
  227. - cmd: python-pip
  228. Let's run it, once:
  229. .. code-block:: bash
  230. salt-call state.apply pep8
  231. The output is:
  232. .. code-block:: text
  233. ----------
  234. State: - pip
  235. Name: pep8
  236. Function: installed
  237. Result: True
  238. Comment: Package was successfully installed
  239. Changes: pep8==1.4.6: Installed
  240. Summary
  241. ------------
  242. Succeeded: 2
  243. Failed: 0
  244. ------------
  245. Total: 2
  246. .. _`pip`: https://pypi.python.org/pypi/pip
  247. .. _`pep8`: https://pypi.python.org/pypi/pep8
  248. .. _`setuptools`: https://pypi.python.org/pypi/setuptools
  249. .. _`runners`: /ref/runners