Skip to content

Commit 7c86d93

Browse files
committed
Merge pull request #241 from whiteinge/doc-statestutfix
Fixed and improved the States tutorials
2 parents 6bbe0cb + 7e5f8be commit 7c86d93

File tree

3 files changed

+205
-125
lines changed

3 files changed

+205
-125
lines changed

doc/topics/tutorials/states_pt1.rst

Lines changed: 106 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,81 +6,152 @@ The purpose of this tutorial is to demonstrate how quickly you can configure a
66
system to be managed by Salt States. For detailed information about the state
77
system please refer to the full :doc:`states reference </ref/states/index>`.
88

9-
This tutorial will walk you through using Salt to configure a single system to
10-
run the Apache HTTP server and to ensure the server is running.
9+
This tutorial will walk you through using Salt to configure a minion to run the
10+
Apache HTTP server and to ensure the server is running.
1111

1212
.. include:: requisite_incl.rst
1313

14-
Create an ``sls`` file
14+
Setting up the Salt State Tree
15+
==============================
16+
17+
States are stored in text files on the master and transfered to the minions on
18+
demand via the master's File Server. The collection of state files make up the
19+
:term:`State Tree`.
20+
21+
To start using a central state system in Salt you must first set up the Salt
22+
File Server. Edit your master config file (:conf_master:`file_roots`) and
23+
uncomment the following lines:
24+
25+
.. code-block:: yaml
26+
27+
file_roots:
28+
base:
29+
- /srv/salt
30+
31+
Restart the Salt master in order to pick up this change:
32+
33+
.. code-block:: bash
34+
35+
% pkill salt-master
36+
% salt-master -d
37+
38+
Preparing the Top File
1539
======================
1640

17-
Start by creating an empty :term:`sls file` named ``webserver.sls``. Type the
18-
following and save the file:
41+
On the master in the directory you specified in the previous step, create a new
42+
file called :conf_master:`top.sls <state_top>` and add the following:
43+
44+
.. code-block:: yaml
45+
46+
base:
47+
'*':
48+
- webserver
49+
50+
The :term:`top file` is separated into environments (discussed later). The
51+
default environment is ``base``. Under the ``base`` environment a collection of
52+
minion matches is defined; for now simply specify all hosts (``*``).
53+
54+
.. admonition:: Targeting minions
55+
56+
The expressions can use any of the targeting mechanisms used by Salt —
57+
minions can be matched by glob, pcre regular expression, or by :doc:`grains
58+
<ref/grains>`. For example::
59+
60+
base:
61+
'os:Fedora':
62+
- match: grain
63+
- webserver
64+
65+
Create an ``sls`` module
66+
========================
67+
68+
In the same directory as your :term:`top file`, create an empty file, called an
69+
:term:`sls module`, named ``webserver.sls``. Type the following and save the
70+
file:
1971

2072
.. code-block:: yaml
2173
:linenos:
2274
23-
apache2: # ID declaration
75+
apache: # ID declaration
2476
pkg: # state declaration
25-
- installed # function
77+
- installed # function declaration
2678
2779
The first line, called the :term:`ID declaration`, is an arbitrary identifier.
28-
In this case it defines the name of the package to be installed. (The exact
29-
package name for the Apache httpd web server may differ on your OS or distro.)
80+
In this case it defines the name of the package to be installed. **NOTE:** the
81+
package name for the Apache httpd web server may differ on your OS or distro —
82+
for example, on Fedora it is ``httpd`` but on Debian/Ubuntu it is ``apache2``.
3083

3184
The second line, called the :term:`state declaration`, defines which of the
3285
Salt States we are using. In this example, we are using the :mod:`pkg state
3386
<salt.states.pkg>` to ensure that a given package is installed.
3487

35-
The third line, called the :term:`function` defines which function in the
36-
:mod:`pkg state <salt.states.pkg>` module to call.
88+
The third line, called the :term:`function declaration` defines which function
89+
in the :mod:`pkg state <salt.states.pkg>` module to call.
3790

3891
.. admonition:: Renderers
3992

4093
States :term:`sls` files can be written in many formats. Salt requires only
4194
a simple data structure and is not concerned with how that data structure
42-
is built. Building the expected structure is the job of Salt
43-
:doc:`renderers </ref/renderers/index>`.
95+
is built. Templating languages and `DSLs`_ are a dime-a-dozen and everyone
96+
has a favorite.
97+
98+
Building the expected data structure is the job of Salt :doc:`renderers
99+
</ref/renderers/index>` and they are dead-simple to write.
44100

45-
In this tutorial we will be using yaml in Jinja2 templates which is the
101+
In this tutorial we will be using YAML in Jinja2 templates which is the
46102
default format. You can change the default by changing
47103
:conf_master:`renderer` in the master configuration file.
48104

105+
.. _`DSLs`: http://en.wikipedia.org/wiki/Domain-specific_language
106+
49107
Install the package
50108
===================
51109

52-
Next, let's run that state. Open a terminal and run:
110+
Next, let's run the state we created. Open a terminal on the master and run:
53111

54112
.. code-block:: bash
55113
56-
% salt '*' state.template /path/to/your/helloworld.sls
114+
% salt '*' state.highstate
57115
58-
:func:`state.template <salt.modules.state.template>` is the simplest way to use
59-
Salt states. It takes the path to a template as an argument and executes it on
60-
the minion.
116+
Our master is instructing all targeted minions to run :func:`state.highstate
117+
<salt.modules.state.highstate>`. When a minion executes a highstate call it
118+
will download the :term:`top file` and attempt to match the expressions. When
119+
it does match an expression the modules listed for it will be downloaded,
120+
compiled, and executed.
61121

62-
You should see a bunch of output as Salt installs Apache.
122+
Once completed, the minion will report back with a summary of all actions taken
123+
and all changes made.
63124

64-
Ensure a service is running
65-
===========================
125+
.. admonition:: Troubleshooting Salt
66126

67-
Let's make a quick modification to also start Apache if it is not running:
127+
In case you don't see the expected output, the following tips can help you
128+
narrow down the problem.
68129

69-
.. code-block:: yaml
70-
:linenos:
71-
:emphasize-lines: 4,5
130+
Turn up logging
131+
Salt can be quite chatty when you change the logging setting to
132+
``debug``::
133+
134+
salt-minion -l debug
135+
136+
Run the minion in the foreground
137+
By not starting the minion in daemon mode (:option:`-d <salt-minion
138+
-d>`) you can view any output from the minion as it works::
139+
140+
salt-minion &
141+
142+
Increase the default timeout value when running :command:`salt`. For
143+
example, to change the default timeout to 60 seconds::
144+
145+
salt -t 60
72146

73-
apache2:
74-
pkg:
75-
- installed
76-
service:
77-
- running
147+
For best results, combine all three::
78148

79-
Run ``state.template`` once again and observe the output.
149+
salt-minion -l debug & # On the minion
150+
salt '*' state.highstate -t 60 # On the master
80151

81152
Next steps
82153
==========
83154

84-
This tutorial focused on using Salt States only on the local system. :doc:`Part
85-
2 <states_pt2>` of the will build on this example to cover using Salt States on
86-
a remote host.
155+
This tutorial focused on getting a simple Salt States configuration working.
156+
:doc:`Part 2 <states_pt2>` will build on this example to cover more advanced
157+
:term:`sls` syntax and will explore more of the states that ship with Salt.

0 commit comments

Comments
 (0)