1
0

states_pt4.rst 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. .. _tutorial-states-part-4:
  2. =======================
  3. States tutorial, part 4
  4. =======================
  5. .. note::
  6. This tutorial builds on topics covered in :ref:`part 1 <states-tutorial>`,
  7. :ref:`part 2 <tutorial-states-part-2>`, and :ref:`part 3 <tutorial-states-part-3>`.
  8. It is recommended that you begin there.
  9. This part of the tutorial will show how to use salt's :conf_master:`file_roots`
  10. to set up a workflow in which states can be "promoted" from dev, to QA, to
  11. production.
  12. Salt fileserver path inheritance
  13. ================================
  14. Salt's fileserver allows for more than one root directory per environment, like
  15. in the below example, which uses both a local directory and a secondary
  16. location shared to the salt master via NFS:
  17. .. code-block:: yaml
  18. # In the master config file (/etc/salt/master)
  19. file_roots:
  20. base:
  21. - /srv/salt
  22. - /mnt/salt-nfs/base
  23. Salt's fileserver collapses the list of root directories into a single virtual
  24. environment containing all files from each root. If the same file exists at the
  25. same relative path in more than one root, then the top-most match "wins". For
  26. example, if ``/srv/salt/foo.txt`` and ``/mnt/salt-nfs/base/foo.txt`` both
  27. exist, then ``salt://foo.txt`` will point to ``/srv/salt/foo.txt``.
  28. .. note::
  29. When using multiple fileserver backends, the order in which they are listed
  30. in the :conf_master:`fileserver_backend` parameter also matters. If both
  31. ``roots`` and ``git`` backends contain a file with the same relative path,
  32. and ``roots`` appears before ``git`` in the
  33. :conf_master:`fileserver_backend` list, then the file in ``roots`` will
  34. "win", and the file in gitfs will be ignored.
  35. A more thorough explanation of how Salt's modular fileserver works can be
  36. found :ref:`here <file-server-backends>`. We recommend reading this.
  37. Environment configuration
  38. =========================
  39. Configure a multiple-environment setup like so:
  40. .. code-block:: yaml
  41. file_roots:
  42. base:
  43. - /srv/salt/prod
  44. qa:
  45. - /srv/salt/qa
  46. - /srv/salt/prod
  47. dev:
  48. - /srv/salt/dev
  49. - /srv/salt/qa
  50. - /srv/salt/prod
  51. Given the path inheritance described above, files within ``/srv/salt/prod``
  52. would be available in all environments. Files within ``/srv/salt/qa`` would be
  53. available in both ``qa``, and ``dev``. Finally, the files within
  54. ``/srv/salt/dev`` would only be available within the ``dev`` environment.
  55. Based on the order in which the roots are defined, new files/states can be
  56. placed within ``/srv/salt/dev``, and pushed out to the dev hosts for testing.
  57. Those files/states can then be moved to the same relative path within
  58. ``/srv/salt/qa``, and they are now available only in the ``dev`` and ``qa``
  59. environments, allowing them to be pushed to QA hosts and tested.
  60. Finally, if moved to the same relative path within ``/srv/salt/prod``, the
  61. files are now available in all three environments.
  62. Requesting files from specific fileserver environments
  63. ======================================================
  64. See :ref:`here <file-server-environments>` for documentation on how to request
  65. files from specific environments.
  66. Practical Example
  67. =================
  68. As an example, consider a simple website, installed to ``/var/www/foobarcom``.
  69. Below is a top.sls that can be used to deploy the website:
  70. ``/srv/salt/prod/top.sls:``
  71. .. code-block:: yaml
  72. base:
  73. 'web*prod*':
  74. - webserver.foobarcom
  75. qa:
  76. 'web*qa*':
  77. - webserver.foobarcom
  78. dev:
  79. 'web*dev*':
  80. - webserver.foobarcom
  81. Using pillar, roles can be assigned to the hosts:
  82. ``/srv/pillar/top.sls:``
  83. .. code-block:: yaml
  84. base:
  85. 'web*prod*':
  86. - webserver.prod
  87. 'web*qa*':
  88. - webserver.qa
  89. 'web*dev*':
  90. - webserver.dev
  91. ``/srv/pillar/webserver/prod.sls:``
  92. .. code-block:: yaml
  93. webserver_role: prod
  94. ``/srv/pillar/webserver/qa.sls:``
  95. .. code-block:: yaml
  96. webserver_role: qa
  97. ``/srv/pillar/webserver/dev.sls:``
  98. .. code-block:: yaml
  99. webserver_role: dev
  100. And finally, the SLS to deploy the website:
  101. ``/srv/salt/prod/webserver/foobarcom.sls:``
  102. .. code-block:: jinja
  103. {% if pillar.get('webserver_role', '') %}
  104. /var/www/foobarcom:
  105. file.recurse:
  106. - source: salt://webserver/src/foobarcom
  107. - env: {{ pillar['webserver_role'] }}
  108. - user: www
  109. - group: www
  110. - dir_mode: 755
  111. - file_mode: 644
  112. {% endif %}
  113. Given the above SLS, the source for the website should initially be placed in
  114. ``/srv/salt/dev/webserver/src/foobarcom``.
  115. First, let's deploy to dev. Given the configuration in the top file, this can
  116. be done using :py:func:`state.apply <salt.modules.state.apply_>`:
  117. .. code-block:: bash
  118. salt --pillar 'webserver_role:dev' state.apply
  119. However, in the event that it is not desirable to apply all states configured
  120. in the top file (which could be likely in more complex setups), it is possible
  121. to apply just the states for the ``foobarcom`` website, by invoking
  122. :py:func:`state.apply <salt.modules.state.apply_>` with the desired SLS target
  123. as an argument:
  124. .. code-block:: bash
  125. salt --pillar 'webserver_role:dev' state.apply webserver.foobarcom
  126. Once the site has been tested in dev, then the files can be moved from
  127. ``/srv/salt/dev/webserver/src/foobarcom`` to
  128. ``/srv/salt/qa/webserver/src/foobarcom``, and deployed using the following:
  129. .. code-block:: bash
  130. salt --pillar 'webserver_role:qa' state.apply webserver.foobarcom
  131. Finally, once the site has been tested in qa, then the files can be moved from
  132. ``/srv/salt/qa/webserver/src/foobarcom`` to
  133. ``/srv/salt/prod/webserver/src/foobarcom``, and deployed using the following:
  134. .. code-block:: bash
  135. salt --pillar 'webserver_role:prod' state.apply webserver.foobarcom
  136. Thanks to Salt's fileserver inheritance, even though the files have been moved
  137. to within ``/srv/salt/prod``, they are still available from the same
  138. ``salt://`` URI in both the qa and dev environments.
  139. Continue Learning
  140. =================
  141. The best way to continue learning about Salt States is to read through the
  142. :ref:`reference documentation <state-system-reference>` and to look through examples
  143. of existing state trees. Many pre-configured state trees
  144. can be found on GitHub in the `saltstack-formulas`_ collection of repositories.
  145. .. _`saltstack-formulas`: https://github.com/saltstack-formulas
  146. If you have any questions, suggestions, or just want to chat with other people
  147. who are using Salt, we have a very :ref:`active community <salt-community>`
  148. and we'd love to hear from you.
  149. In addition, by continuing to the :ref:`Orchestrate Runner <orchestrate-runner>` docs,
  150. you can learn about the powerful orchestration of which Salt is capable.