2016-12-15 22:36:44 +00:00
|
|
|
.. _tutorial-states-part-2:
|
|
|
|
|
2014-03-14 02:02:33 +00:00
|
|
|
=========================================================
|
|
|
|
States tutorial, part 2 - More Complex States, Requisites
|
|
|
|
=========================================================
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2014-12-11 15:50:14 +00:00
|
|
|
.. note::
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2016-12-15 22:36:44 +00:00
|
|
|
This tutorial builds on topics covered in :ref:`part 1 <states-tutorial>`. It is
|
2014-07-14 17:34:03 +00:00
|
|
|
recommended that you begin there.
|
2012-08-03 06:23:39 +00:00
|
|
|
|
2016-12-15 22:36:44 +00:00
|
|
|
In the :ref:`last part <states-tutorial>` of the Salt States tutorial we covered the
|
2014-07-14 17:34:03 +00:00
|
|
|
basics of installing a package. We will now modify our ``webserver.sls`` file
|
|
|
|
to have requirements, and use even more Salt States.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
Call multiple States
|
|
|
|
====================
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2014-02-28 01:27:07 +00:00
|
|
|
You can specify multiple :ref:`state-declaration` under an
|
|
|
|
:ref:`id-declaration`. For example, a quick modification to our
|
2011-11-26 08:52:59 +00:00
|
|
|
``webserver.sls`` to also start Apache if it is not running:
|
2011-10-30 16:21:11 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
2011-11-16 16:20:59 +00:00
|
|
|
:linenos:
|
|
|
|
:emphasize-lines: 4,5
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
apache:
|
2014-12-13 06:29:48 +00:00
|
|
|
pkg.installed: []
|
|
|
|
service.running:
|
2013-05-03 02:49:23 +00:00
|
|
|
- require:
|
|
|
|
- pkg: apache
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2016-03-22 03:56:17 +00:00
|
|
|
Try stopping Apache before running :py:func:`state.apply
|
|
|
|
<salt.modules.state.apply_>` once again and observe the output.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2015-10-30 16:47:46 +00:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
For those running RedhatOS derivatives (Centos, AWS), you will want to specify the
|
|
|
|
service name to be httpd. More on state service here, :mod:`service state
|
|
|
|
<salt.states.service>`. With the example above, just add "- name: httpd"
|
|
|
|
above the require line and with the same spacing.
|
|
|
|
|
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
Require other states
|
2011-10-30 16:21:11 +00:00
|
|
|
====================
|
|
|
|
|
2011-10-31 01:23:12 +00:00
|
|
|
We now have a working installation of Apache so let's add an HTML file to
|
2011-11-16 16:20:59 +00:00
|
|
|
customize our website. It isn't exactly useful to have a website without a
|
|
|
|
webserver so we don't want Salt to install our HTML file until Apache is
|
|
|
|
installed and running. Include the following at the bottom of your
|
|
|
|
``webserver/init.sls`` file:
|
2011-10-30 16:21:11 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
2011-10-31 01:23:12 +00:00
|
|
|
:linenos:
|
2012-02-28 04:19:29 +00:00
|
|
|
:emphasize-lines: 7,11
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
apache:
|
2014-12-13 06:29:48 +00:00
|
|
|
pkg.installed: []
|
|
|
|
service.running:
|
2013-05-03 02:49:23 +00:00
|
|
|
- require:
|
|
|
|
- pkg: apache
|
2011-11-16 16:20:59 +00:00
|
|
|
|
|
|
|
/var/www/index.html: # ID declaration
|
|
|
|
file: # state declaration
|
|
|
|
- managed # function
|
|
|
|
- source: salt://webserver/index.html # function arg
|
|
|
|
- require: # requisite declaration
|
|
|
|
- pkg: apache # requisite reference
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2014-12-24 00:20:42 +00:00
|
|
|
**line 7** is the :ref:`id-declaration`. In this example it is the location we
|
2014-02-28 01:27:07 +00:00
|
|
|
want to install our custom HTML file. (**Note:** the default location that
|
|
|
|
Apache serves may differ from the above on your OS or distro. ``/srv/www``
|
|
|
|
could also be a likely place to look.)
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2014-12-24 00:20:42 +00:00
|
|
|
**Line 8** the :ref:`state-declaration`. This example uses the Salt :mod:`file
|
2011-10-31 01:23:12 +00:00
|
|
|
state <salt.states.file>`.
|
|
|
|
|
2014-12-24 00:20:42 +00:00
|
|
|
**Line 9** is the :ref:`function-declaration`. The :func:`managed function
|
2011-10-31 01:23:12 +00:00
|
|
|
<salt.states.file.managed>` will download a file from the master and install it
|
|
|
|
in the location specified.
|
|
|
|
|
2014-12-24 00:20:42 +00:00
|
|
|
**Line 10** is a :ref:`function-arg-declaration` which, in this example, passes
|
2011-10-31 01:23:12 +00:00
|
|
|
the ``source`` argument to the :func:`managed function
|
2012-02-28 04:19:29 +00:00
|
|
|
<salt.states.file.managed>`.
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2014-12-24 00:20:42 +00:00
|
|
|
**Line 11** is a :ref:`requisite-declaration`.
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2014-12-24 00:20:42 +00:00
|
|
|
**Line 12** is a :ref:`requisite-reference` which refers to a state and an ID.
|
2011-10-31 01:23:12 +00:00
|
|
|
In this example, it is referring to the ``ID declaration`` from our example in
|
2016-12-15 22:36:44 +00:00
|
|
|
:ref:`part 1 <states-tutorial>`. This declaration tells Salt not to install the HTML
|
2011-10-31 01:23:12 +00:00
|
|
|
file until Apache is installed.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
Next, create the ``index.html`` file and save it in the ``webserver``
|
|
|
|
directory:
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-10-31 01:23:12 +00:00
|
|
|
.. code-block:: html
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2014-12-24 00:20:42 +00:00
|
|
|
<!DOCTYPE html>
|
2011-10-31 01:23:12 +00:00
|
|
|
<html>
|
|
|
|
<head><title>Salt rocks</title></head>
|
|
|
|
<body>
|
|
|
|
<h1>This file brought to you by Salt</h1>
|
|
|
|
</body>
|
|
|
|
</html>
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2016-03-22 03:56:17 +00:00
|
|
|
Last, call :func:`state.apply <salt.modules.state.apply_>` again and the minion
|
|
|
|
will fetch and execute the :ref:`highstate <running-highstate>` as well as our
|
|
|
|
HTML file from the master using Salt's File Server:
|
2013-08-12 03:17:47 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2016-03-22 03:56:17 +00:00
|
|
|
salt '*' state.apply
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-10-31 01:23:12 +00:00
|
|
|
Verify that Apache is now serving your custom HTML.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
.. admonition:: ``require`` vs. ``watch``
|
|
|
|
|
2014-12-12 19:32:58 +00:00
|
|
|
There are two :ref:`requisite-declaration`, “require”, and “watch”. Not
|
2014-02-28 01:27:07 +00:00
|
|
|
every state supports “watch”. The :mod:`service state
|
|
|
|
<salt.states.service>` does support “watch” and will restart a service
|
|
|
|
based on the watch condition.
|
2011-11-16 16:20:59 +00:00
|
|
|
|
|
|
|
For example, if you use Salt to install an Apache virtual host
|
2011-11-16 21:16:39 +00:00
|
|
|
configuration file and want to restart Apache whenever that file is changed
|
|
|
|
you could modify our Apache example from earlier as follows:
|
2011-11-16 16:20:59 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
2011-11-16 21:16:39 +00:00
|
|
|
:emphasize-lines: 1,2,3,4,11,12
|
|
|
|
|
|
|
|
/etc/httpd/extra/httpd-vhosts.conf:
|
2014-12-13 06:29:48 +00:00
|
|
|
file.managed:
|
2011-11-16 21:16:39 +00:00
|
|
|
- source: salt://webserver/httpd-vhosts.conf
|
2011-11-16 16:20:59 +00:00
|
|
|
|
|
|
|
apache:
|
2014-12-13 06:29:48 +00:00
|
|
|
pkg.installed: []
|
|
|
|
service.running:
|
2011-11-16 21:16:39 +00:00
|
|
|
- watch:
|
|
|
|
- file: /etc/httpd/extra/httpd-vhosts.conf
|
2013-05-03 02:49:23 +00:00
|
|
|
- require:
|
|
|
|
- pkg: apache
|
2011-11-16 21:16:39 +00:00
|
|
|
|
|
|
|
If the pkg and service names differ on your OS or distro of choice you can
|
2014-02-28 01:27:07 +00:00
|
|
|
specify each one separately using a :ref:`name-declaration` which explained
|
2016-12-15 22:36:44 +00:00
|
|
|
in :ref:`Part 3 <tutorial-states-part-3>`.
|
2011-11-16 16:20:59 +00:00
|
|
|
|
|
|
|
Next steps
|
|
|
|
==========
|
|
|
|
|
2016-12-15 22:36:44 +00:00
|
|
|
In :ref:`part 3 <tutorial-states-part-3>` we will discuss how to use includes, extends, and
|
2012-08-03 06:23:39 +00:00
|
|
|
templating to make a more complete State Tree configuration.
|