states_pt2.rst 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. .. _tutorial-states-part-2:
  2. =========================================================
  3. States tutorial, part 2 - More Complex States, Requisites
  4. =========================================================
  5. .. note::
  6. This tutorial builds on topics covered in :ref:`part 1 <states-tutorial>`. It is
  7. recommended that you begin there.
  8. In the :ref:`last part <states-tutorial>` of the Salt States tutorial we covered the
  9. basics of installing a package. We will now modify our ``webserver.sls`` file
  10. to have requirements, and use even more Salt States.
  11. Call multiple States
  12. ====================
  13. You can specify multiple :ref:`state-declaration` under an
  14. :ref:`id-declaration`. For example, a quick modification to our
  15. ``webserver.sls`` to also start Apache if it is not running:
  16. .. code-block:: yaml
  17. :linenos:
  18. :emphasize-lines: 4,5
  19. apache:
  20. pkg.installed: []
  21. service.running:
  22. - require:
  23. - pkg: apache
  24. Try stopping Apache before running :py:func:`state.apply
  25. <salt.modules.state.apply_>` once again and observe the output.
  26. .. note::
  27. For those running RedhatOS derivatives (Centos, AWS), you will want to specify the
  28. service name to be httpd. More on state service here, :mod:`service state
  29. <salt.states.service>`. With the example above, just add "- name: httpd"
  30. above the require line and with the same spacing.
  31. Require other states
  32. ====================
  33. We now have a working installation of Apache so let's add an HTML file to
  34. customize our website. It isn't exactly useful to have a website without a
  35. webserver so we don't want Salt to install our HTML file until Apache is
  36. installed and running. Include the following at the bottom of your
  37. ``webserver/init.sls`` file:
  38. .. code-block:: yaml
  39. :linenos:
  40. :emphasize-lines: 7,11
  41. apache:
  42. pkg.installed: []
  43. service.running:
  44. - require:
  45. - pkg: apache
  46. /var/www/index.html: # ID declaration
  47. file: # state declaration
  48. - managed # function
  49. - source: salt://webserver/index.html # function arg
  50. - require: # requisite declaration
  51. - pkg: apache # requisite reference
  52. **line 7** is the :ref:`id-declaration`. In this example it is the location we
  53. want to install our custom HTML file. (**Note:** the default location that
  54. Apache serves may differ from the above on your OS or distro. ``/srv/www``
  55. could also be a likely place to look.)
  56. **Line 8** the :ref:`state-declaration`. This example uses the Salt :mod:`file
  57. state <salt.states.file>`.
  58. **Line 9** is the :ref:`function-declaration`. The :func:`managed function
  59. <salt.states.file.managed>` will download a file from the master and install it
  60. in the location specified.
  61. **Line 10** is a :ref:`function-arg-declaration` which, in this example, passes
  62. the ``source`` argument to the :func:`managed function
  63. <salt.states.file.managed>`.
  64. **Line 11** is a :ref:`requisite-declaration`.
  65. **Line 12** is a :ref:`requisite-reference` which refers to a state and an ID.
  66. In this example, it is referring to the ``ID declaration`` from our example in
  67. :ref:`part 1 <states-tutorial>`. This declaration tells Salt not to install the HTML
  68. file until Apache is installed.
  69. Next, create the ``index.html`` file and save it in the ``webserver``
  70. directory:
  71. .. code-block:: html
  72. <!DOCTYPE html>
  73. <html>
  74. <head><title>Salt rocks</title></head>
  75. <body>
  76. <h1>This file brought to you by Salt</h1>
  77. </body>
  78. </html>
  79. Last, call :func:`state.apply <salt.modules.state.apply_>` again and the minion
  80. will fetch and execute the :ref:`highstate <running-highstate>` as well as our
  81. HTML file from the master using Salt's File Server:
  82. .. code-block:: bash
  83. salt '*' state.apply
  84. Verify that Apache is now serving your custom HTML.
  85. .. admonition:: ``require`` vs. ``watch``
  86. There are two :ref:`requisite-declaration`, “require”, and “watch”. Not
  87. every state supports “watch”. The :mod:`service state
  88. <salt.states.service>` does support “watch” and will restart a service
  89. based on the watch condition.
  90. For example, if you use Salt to install an Apache virtual host
  91. configuration file and want to restart Apache whenever that file is changed
  92. you could modify our Apache example from earlier as follows:
  93. .. code-block:: yaml
  94. :emphasize-lines: 1,2,3,10,11
  95. /etc/httpd/extra/httpd-vhosts.conf:
  96. file.managed:
  97. - source: salt://webserver/httpd-vhosts.conf
  98. apache:
  99. pkg.installed: []
  100. service.running:
  101. - watch:
  102. - file: /etc/httpd/extra/httpd-vhosts.conf
  103. - require:
  104. - pkg: apache
  105. If the pkg and service names differ on your OS or distro of choice you can
  106. specify each one separately using a :ref:`name-declaration` which explained
  107. in :ref:`Part 3 <tutorial-states-part-3>`.
  108. Next steps
  109. ==========
  110. In :ref:`part 3 <tutorial-states-part-3>` we will discuss how to use includes, extends, and
  111. templating to make a more complete State Tree configuration.