2011-10-30 16:21:11 +00:00
|
|
|
=======================
|
|
|
|
States tutorial, part 1
|
|
|
|
=======================
|
|
|
|
|
|
|
|
The purpose of this tutorial is to demonstrate how quickly you can configure a
|
|
|
|
system to be managed by Salt States. For detailed information about the state
|
|
|
|
system please refer to the full :doc:`states reference </ref/states/index>`.
|
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
This tutorial will walk you through using Salt to configure a minion to run the
|
|
|
|
Apache HTTP server and to ensure the server is running.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2013-01-24 06:46:12 +00:00
|
|
|
.. include:: /_incl/requisite_incl.rst
|
2011-10-30 20:33:24 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
Setting up the Salt State Tree
|
|
|
|
==============================
|
|
|
|
|
|
|
|
States are stored in text files on the master and transfered to the minions on
|
|
|
|
demand via the master's File Server. The collection of state files make up the
|
|
|
|
:term:`State Tree`.
|
|
|
|
|
|
|
|
To start using a central state system in Salt you must first set up the Salt
|
|
|
|
File Server. Edit your master config file (:conf_master:`file_roots`) and
|
|
|
|
uncomment the following lines:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
file_roots:
|
|
|
|
base:
|
|
|
|
- /srv/salt
|
|
|
|
|
2012-02-29 20:45:46 +00:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
If you are deploying on FreeBSD via ports, the ``file_roots`` path defaults
|
|
|
|
to ``/usr/local/etc/salt/states``.
|
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
Restart the Salt master in order to pick up this change:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
% pkill salt-master
|
|
|
|
% salt-master -d
|
|
|
|
|
|
|
|
Preparing the Top File
|
2011-10-30 16:21:11 +00:00
|
|
|
======================
|
|
|
|
|
2012-06-05 20:54:21 +00:00
|
|
|
On the master in the directory you uncommented in the previous step
|
|
|
|
(``/srv/salt`` by default), create a new file called
|
|
|
|
:conf_master:`top.sls <state_top>` and add the following:
|
2011-11-16 16:20:59 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
base:
|
|
|
|
'*':
|
|
|
|
- webserver
|
|
|
|
|
|
|
|
The :term:`top file` is separated into environments (discussed later). The
|
|
|
|
default environment is ``base``. Under the ``base`` environment a collection of
|
|
|
|
minion matches is defined; for now simply specify all hosts (``*``).
|
|
|
|
|
|
|
|
.. admonition:: Targeting minions
|
|
|
|
|
|
|
|
The expressions can use any of the targeting mechanisms used by Salt —
|
|
|
|
minions can be matched by glob, pcre regular expression, or by :doc:`grains
|
2012-03-19 11:46:20 +00:00
|
|
|
</topics/targeting/grains>`. For example::
|
2011-11-16 16:20:59 +00:00
|
|
|
|
|
|
|
base:
|
|
|
|
'os:Fedora':
|
|
|
|
- match: grain
|
|
|
|
- webserver
|
|
|
|
|
|
|
|
Create an ``sls`` module
|
|
|
|
========================
|
|
|
|
|
|
|
|
In the same directory as your :term:`top file`, create an empty file, called an
|
2012-05-23 04:43:12 +00:00
|
|
|
:term:`SLS module`, named ``webserver.sls``. Type the following and save the
|
2011-11-16 16:20:59 +00:00
|
|
|
file:
|
2011-10-30 16:21:11 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
2011-10-31 01:23:12 +00:00
|
|
|
:linenos:
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
apache: # ID declaration
|
2011-10-30 16:21:11 +00:00
|
|
|
pkg: # state declaration
|
2011-11-16 16:20:59 +00:00
|
|
|
- installed # function declaration
|
2011-10-30 16:21:11 +00:00
|
|
|
|
|
|
|
The first line, called the :term:`ID declaration`, is an arbitrary identifier.
|
2011-11-16 16:20:59 +00:00
|
|
|
In this case it defines the name of the package to be installed. **NOTE:** the
|
|
|
|
package name for the Apache httpd web server may differ on your OS or distro —
|
|
|
|
for example, on Fedora it is ``httpd`` but on Debian/Ubuntu it is ``apache2``.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
|
|
|
The second line, called the :term:`state declaration`, defines which of the
|
|
|
|
Salt States we are using. In this example, we are using the :mod:`pkg state
|
|
|
|
<salt.states.pkg>` to ensure that a given package is installed.
|
|
|
|
|
2012-03-09 04:05:52 +00:00
|
|
|
The third line, called the :term:`function declaration`, defines which function
|
2011-11-16 16:20:59 +00:00
|
|
|
in the :mod:`pkg state <salt.states.pkg>` module to call.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
|
|
|
.. admonition:: Renderers
|
|
|
|
|
|
|
|
States :term:`sls` files can be written in many formats. Salt requires only
|
|
|
|
a simple data structure and is not concerned with how that data structure
|
2011-11-16 16:20:59 +00:00
|
|
|
is built. Templating languages and `DSLs`_ are a dime-a-dozen and everyone
|
|
|
|
has a favorite.
|
|
|
|
|
|
|
|
Building the expected data structure is the job of Salt :doc:`renderers
|
|
|
|
</ref/renderers/index>` and they are dead-simple to write.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2012-03-09 04:05:52 +00:00
|
|
|
In this tutorial we will be using YAML in Jinja2 templates, which is the
|
2011-10-30 16:21:11 +00:00
|
|
|
default format. You can change the default by changing
|
2011-10-31 01:23:12 +00:00
|
|
|
:conf_master:`renderer` in the master configuration file.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
.. _`DSLs`: http://en.wikipedia.org/wiki/Domain-specific_language
|
|
|
|
|
2011-10-30 16:21:11 +00:00
|
|
|
Install the package
|
|
|
|
===================
|
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
Next, let's run the state we created. Open a terminal on the master and run:
|
2011-10-30 16:21:11 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
% salt '*' state.highstate
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
Our master is instructing all targeted minions to run :func:`state.highstate
|
|
|
|
<salt.modules.state.highstate>`. When a minion executes a highstate call it
|
|
|
|
will download the :term:`top file` and attempt to match the expressions. When
|
|
|
|
it does match an expression the modules listed for it will be downloaded,
|
|
|
|
compiled, and executed.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
Once completed, the minion will report back with a summary of all actions taken
|
|
|
|
and all changes made.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
.. admonition:: Troubleshooting Salt
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
In case you don't see the expected output, the following tips can help you
|
|
|
|
narrow down the problem.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
Turn up logging
|
|
|
|
Salt can be quite chatty when you change the logging setting to
|
|
|
|
``debug``::
|
|
|
|
|
|
|
|
salt-minion -l debug
|
|
|
|
|
|
|
|
Run the minion in the foreground
|
|
|
|
By not starting the minion in daemon mode (:option:`-d <salt-minion
|
|
|
|
-d>`) you can view any output from the minion as it works::
|
|
|
|
|
|
|
|
salt-minion &
|
|
|
|
|
|
|
|
Increase the default timeout value when running :command:`salt`. For
|
|
|
|
example, to change the default timeout to 60 seconds::
|
|
|
|
|
|
|
|
salt -t 60
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
For best results, combine all three::
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
salt-minion -l debug & # On the minion
|
|
|
|
salt '*' state.highstate -t 60 # On the master
|
2011-10-30 16:21:11 +00:00
|
|
|
|
|
|
|
Next steps
|
|
|
|
==========
|
|
|
|
|
2011-11-16 16:20:59 +00:00
|
|
|
This tutorial focused on getting a simple Salt States configuration working.
|
|
|
|
:doc:`Part 2 <states_pt2>` will build on this example to cover more advanced
|
|
|
|
:term:`sls` syntax and will explore more of the states that ship with Salt.
|