states_pt1.rst 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. .. _states-tutorial:
  2. =====================================
  3. States tutorial, part 1 - Basic Usage
  4. =====================================
  5. The purpose of this tutorial is to demonstrate how quickly you can configure a
  6. system to be managed by Salt States. For detailed information about the state
  7. system please refer to the full :ref:`states reference <state-system-reference>`.
  8. This tutorial will walk you through using Salt to configure a minion to run the
  9. Apache HTTP server and to ensure the server is running.
  10. .. include:: /_incl/requisite_incl.rst
  11. Setting up the Salt State Tree
  12. ==============================
  13. States are stored in text files on the master and transferred to the minions on
  14. demand via the master's File Server. The collection of state files make up the
  15. ``State Tree``.
  16. To start using a central state system in Salt, the Salt File Server must first
  17. be set up. Edit the master config file (:conf_master:`file_roots`) and
  18. uncomment the following lines:
  19. .. code-block:: yaml
  20. file_roots:
  21. base:
  22. - /srv/salt
  23. .. note::
  24. If you are deploying on FreeBSD via ports, the ``file_roots`` path defaults
  25. to ``/usr/local/etc/salt/states``.
  26. Restart the Salt master in order to pick up this change:
  27. .. code-block:: bash
  28. pkill salt-master
  29. salt-master -d
  30. Preparing the Top File
  31. ======================
  32. On the master, in the directory uncommented in the previous step,
  33. (``/srv/salt`` by default), create a new file called
  34. :conf_master:`top.sls <state_top>` and add the following:
  35. .. code-block:: yaml
  36. base:
  37. '*':
  38. - webserver
  39. The :ref:`top file <states-top>` is separated into environments (discussed
  40. later). The default environment is ``base``. Under the ``base`` environment a
  41. collection of minion matches is defined; for now simply specify all hosts
  42. (``*``).
  43. .. _targeting-minions:
  44. .. admonition:: Targeting minions
  45. The expressions can use any of the targeting mechanisms used by Salt —
  46. minions can be matched by glob, PCRE regular expression, or by :ref:`grains
  47. <targeting-grains>`. For example:
  48. .. code-block:: yaml
  49. base:
  50. 'os:Fedora':
  51. - match: grain
  52. - webserver
  53. Create an ``sls`` file
  54. ======================
  55. In the same directory as the :ref:`top file <states-top>`, create a file
  56. named ``webserver.sls``, containing the following:
  57. .. code-block:: yaml
  58. apache: # ID declaration
  59. pkg: # state declaration
  60. - installed # function declaration
  61. The first line, called the :ref:`id-declaration`, is an arbitrary identifier.
  62. In this case it defines the name of the package to be installed.
  63. .. note::
  64. The package name for the Apache httpd web server may differ depending on
  65. OS or distro — for example, on Fedora it is ``httpd`` but on
  66. Debian/Ubuntu it is ``apache2``.
  67. The second line, called the :ref:`state-declaration`, defines which of the Salt
  68. States we are using. In this example, we are using the :mod:`pkg state
  69. <salt.states.pkg>` to ensure that a given package is installed.
  70. The third line, called the :ref:`function-declaration`, defines which function
  71. in the :mod:`pkg state <salt.states.pkg>` module to call.
  72. .. admonition:: Renderers
  73. States ``sls`` files can be written in many formats. Salt requires only
  74. a simple data structure and is not concerned with how that data structure
  75. is built. Templating languages and `DSLs`_ are a dime-a-dozen and everyone
  76. has a favorite.
  77. Building the expected data structure is the job of Salt :ref:`renderers`
  78. and they are dead-simple to write.
  79. In this tutorial we will be using YAML in Jinja2 templates, which is the
  80. default format. The default can be changed by editing
  81. :conf_master:`renderer` in the master configuration file.
  82. .. _`DSLs`: https://en.wikipedia.org/wiki/Domain-specific_language
  83. .. _running-highstate:
  84. Install the package
  85. ===================
  86. Next, let's run the state we created. Open a terminal on the master and run:
  87. .. code-block:: bash
  88. salt '*' state.apply
  89. Our master is instructing all targeted minions to run :func:`state.apply
  90. <salt.modules.state.apply>`. When this function is executed without any SLS
  91. targets, a minion will download the :ref:`top file <states-top>` and attempt to
  92. match the expressions within it. When the minion does match an expression the
  93. modules listed for it will be downloaded, compiled, and executed.
  94. .. note::
  95. This action is referred to as a "highstate", and can be run using the
  96. :py:func:`state.highstate <salt.modules.state.highstate>` function.
  97. However, to make the usage easier to understand ("highstate" is not
  98. necessarily an intuitive name), a :py:func:`state.apply
  99. <salt.modules.state.apply_>` function was added in version 2015.5.0, which
  100. when invoked without any SLS names will trigger a highstate.
  101. :py:func:`state.highstate <salt.modules.state.highstate>` still exists and
  102. can be used, but the documentation (as can be seen above) has been updated
  103. to reference :py:func:`state.apply <salt.modules.state.apply_>`, so keep
  104. the following in mind as you read the documentation:
  105. - :py:func:`state.apply <salt.modules.state.apply_>` invoked without any
  106. SLS names will run :py:func:`state.highstate
  107. <salt.modules.state.highstate>`
  108. - :py:func:`state.apply <salt.modules.state.apply_>` invoked with SLS names
  109. will run :py:func:`state.sls <salt.modules.state.sls>`
  110. Once completed, the minion will report back with a summary of all actions taken
  111. and all changes made.
  112. .. warning::
  113. If you have created :ref:`custom grain modules <writing-grains>`, they will
  114. not be available in the top file until after the first :ref:`highstate
  115. <running-highstate>`. To make custom grains available on a minion's first
  116. :ref:`highstate <running-highstate>`, it is recommended to use :ref:`this
  117. example <minion-start-reactor>` to ensure that the custom grains are synced
  118. when the minion starts.
  119. .. _sls-file-namespace:
  120. .. admonition:: SLS File Namespace
  121. Note that in the :ref:`example <targeting-minions>` above, the SLS file
  122. ``webserver.sls`` was referred to simply as ``webserver``. The namespace
  123. for SLS files when referenced in :conf_master:`top.sls <state_top>` or an :ref:`include-declaration`
  124. follows a few simple rules:
  125. 1. The ``.sls`` is discarded (i.e. ``webserver.sls`` becomes
  126. ``webserver``).
  127. 2. Subdirectories can be used for better organization.
  128. a. Each subdirectory is represented with a dot (following the Python
  129. import model) in Salt states and on the command line . ``webserver/dev.sls``
  130. on the filesystem is referred to as ``webserver.dev`` in Salt
  131. b. Because slashes are represented as dots, SLS files can not contain
  132. dots in the name (other than the dot for the SLS suffix). The SLS
  133. file ``webserver_1.0.sls`` can not be matched, and ``webserver_1.0``
  134. would match the directory/file ``webserver_1/0.sls``
  135. 3. A file called ``init.sls`` in a subdirectory is referred to by the path
  136. of the directory. So, ``webserver/init.sls`` is referred to as
  137. ``webserver``.
  138. 4. If both ``webserver.sls`` and ``webserver/init.sls`` happen to exist,
  139. ``webserver/init.sls`` will be ignored and ``webserver.sls`` will be the
  140. file referred to as ``webserver``.
  141. .. admonition:: Troubleshooting Salt
  142. If the expected output isn't seen, the following tips can help to
  143. narrow down the problem.
  144. Turn up logging
  145. Salt can be quite chatty when you change the logging setting to
  146. ``debug``:
  147. .. code-block:: bash
  148. salt-minion -l debug
  149. Run the minion in the foreground
  150. By not starting the minion in daemon mode (:option:`-d <salt-minion -d>`)
  151. one can view any output from the minion as it works:
  152. .. code-block:: bash
  153. salt-minion
  154. Increase the default timeout value when running :command:`salt`. For
  155. example, to change the default timeout to 60 seconds:
  156. .. code-block:: bash
  157. salt -t 60
  158. For best results, combine all three:
  159. .. code-block:: bash
  160. salt-minion -l debug # On the minion
  161. salt '*' state.apply -t 60 # On the master
  162. Next steps
  163. ==========
  164. This tutorial focused on getting a simple Salt States configuration working.
  165. :ref:`Part 2 <tutorial-states-part-2>` will build on this example to cover more advanced
  166. ``sls`` syntax and will explore more of the states that ship with Salt.