salt/doc/ref/states/ordering.rst

319 lines
10 KiB
ReStructuredText
Raw Normal View History

2011-11-24 23:21:08 +00:00
===============
Ordering States
===============
2013-08-20 17:52:10 +00:00
The way in which configuration management systems are executed is a hotly
debated topic in the configuration management world. Two
major philosophies exist on the subject, to either execute in an imperative
fashion where things are executed in the order in which they are defined, or
in a declarative fashion where dependencies need to be mapped between objects.
2011-11-24 23:21:08 +00:00
2013-08-20 17:52:10 +00:00
Imperative ordering is finite and generally considered easier to write, but
2013-08-20 19:21:26 +00:00
declarative ordering is much more powerful and flexible but generally considered
2013-08-20 17:52:10 +00:00
more difficult to create.
2011-11-24 23:21:08 +00:00
2013-08-20 19:21:26 +00:00
Salt has been created to get the best of both worlds. States are evaluated in
a finite order, which guarantees that states are always executed in the same
order, and the states runtime is declarative, making Salt fully aware of
2013-10-24 13:40:21 +00:00
dependencies via the `requisite` system.
2013-08-20 17:52:10 +00:00
State Auto Ordering
===================
2013-09-03 12:59:14 +00:00
.. versionadded: 0.17.0
2013-08-20 17:52:10 +00:00
Salt always executes states in a finite manner, meaning that they will always
2013-08-20 19:21:26 +00:00
execute in the same order regardless of the system that is executing them.
But in Salt 0.17.0, the ``state_auto_order`` option was added. This option
2013-08-20 17:52:10 +00:00
makes states get evaluated in the order in which they are defined in sls
files.
The evaluation order makes it easy to know what order the states will be
executed in, but it is important to note that the requisite system will
2013-08-20 19:21:26 +00:00
override the ordering defined in the files, and the ``order`` option described
below will also override the order in which states are defined in sls files.
2013-10-24 13:40:21 +00:00
If the classic ordering is preferred (lexicographic), then set
``state_auto_order`` to ``False`` in the master configuration file.
2012-02-17 18:41:48 +00:00
Requisite Statements
====================
.. note::
This document represents behavior exhibited by Salt requisites as of
version 0.9.7 of Salt.
Often when setting up states any single action will require or depend on
2013-10-24 13:40:21 +00:00
another action. Salt allows for the building of relationships between states
with requisite statements. A requisite statement ensures that the named state
is evaluated before the state requiring it. There are three types of requisite
statements in Salt, **require**, **watch** and **prereq**.
2012-02-17 18:41:48 +00:00
These requisite statements are applied to a specific state declaration:
.. code-block:: yaml
httpd:
pkg:
- installed
file.managed:
2012-02-17 18:41:48 +00:00
- name: /etc/httpd/conf/httpd.conf
- source: salt://httpd/httpd.conf
- require:
- pkg: httpd
In this example, the **require** requisite is used to declare that the file
2012-02-17 18:41:48 +00:00
/etc/httpd/conf/httpd.conf should only be set up if the pkg state executes
successfully.
The requisite system works by finding the states that are required and
executing them before the state that requires them. Then the required states
can be evaluated to see if they have executed correctly.
Require statements can refer to the following requisite types: pkg, file, sls
In addition to state declarations such as pkg, file, etc., **sls** type requisites
are also recognized, and essentially allow 'chaining' of states. This provides a
mechanism to ensure the proper sequence for complex state formulas, especially when
the discrete states are split or groups into separate sls files:
.. code-block:: yaml
include:
- network
httpd:
pkg:
- installed
service:
- running
- require:
- pkg: httpd
- sls: network
In this example, the httpd sevice running state will not be applied
(i.e., the httpd service will not be started) unless both the https package is
installed AND the network state is satistifed.
2013-02-14 03:34:21 +00:00
.. note:: Requisite matching
Requisites match on both the ID Declaration and the ``name`` parameter.
2013-10-24 13:40:21 +00:00
Therefore, if using the ``pkgs`` or ``sources`` argument to install
a list of packages in a pkg state, it's important to note that it is
imposible to match an individual package in the list, since all packages
are installed as a single state.
2013-02-14 03:34:21 +00:00
2012-02-17 18:41:48 +00:00
Multiple Requisites
-------------------
The requisite statement is passed as a list, allowing for the easy addition of
more requisites. Both requisite types can also be separately declared:
.. code-block:: yaml
httpd:
pkg:
- installed
service.running:
2012-02-17 18:41:48 +00:00
- enable: True
- watch:
- file: /etc/httpd/conf/httpd.conf
- require:
- pkg: httpd
- user: httpd
- group: httpd
file.managed:
2012-02-17 18:41:48 +00:00
- name: /etc/httpd/conf/httpd.conf
- source: salt://httpd/httpd.conf
- require:
- pkg: httpd
user:
- present
group:
- present
2012-02-17 18:41:48 +00:00
In this example, the httpd service is only going to be started if the package,
2012-02-17 18:41:48 +00:00
user, group and file are executed successfully.
The Require Requisite
---------------------
The foundation of the requisite system is the ``require`` requisite. The
require requisite ensures that the required state(s) are executed before the
requiring state. So, if a state is declared that sets down a vimrc, then it
would be pertinent to make sure that the vimrc file would only be set down if
the vim package has been installed:
.. code-block:: yaml
vim:
pkg:
- installed
file.managed:
2012-02-17 18:41:48 +00:00
- source: salt://vim/vimrc
- require:
- pkg: vim
In this case, the vimrc file will only be applied by Salt if and after the vim
package is installed.
The Watch Requisite
-------------------
The ``watch`` requisite is more advanced than the ``require`` requisite. The
watch requisite executes the same logic as require (therefore if something is
watched it does not need to also be required) with the addition of executing
logic if the required states have changed in some way.
The watch requisite checks to see if the watched states have returned any
changes. If the watched state returns changes, and the watched states execute
successfully, then the watching state will execute a function that reacts to
the changes in the watched states.
Perhaps an example can better explain the behavior:
.. code-block:: yaml
redis:
pkg:
- latest
file.managed:
2012-02-17 18:41:48 +00:00
- source: salt://redis/redis.conf
- name: /etc/redis.conf
- require:
- pkg: redis
service.running:
2012-02-17 18:41:48 +00:00
- enable: True
- watch:
- file: /etc/redis.conf
- pkg: redis
In this example, the redis service will only be started if the file
2012-02-17 18:41:48 +00:00
/etc/redis.conf is applied, and the file is only applied if the package is
installed. This is normal require behavior, but if the watched file changes,
or the watched package is installed or upgraded, then the redis service is
restarted.
Watch and the mod_watch Function
--------------------------------
2012-02-17 18:41:48 +00:00
The watch requisite is based on the ``mod_watch`` function. Python state
modules can include a function called ``mod_watch`` which is then called
2012-11-24 07:07:53 +00:00
if the watch call is invoked. When ``mod_watch`` is called depends on the
execution of the watched state, which:
- If no changes then just run the watching state itself as usual.
``mod_watch`` is not called. This behavior is same as using a ``require``.
- If changes then run the watching state *AND* if that changes nothing then
react by calling ``mod_watch``.
When reacting, in the case of the service module the underlying service is
restarted. In the case of the cmd state the command is executed.
2012-02-17 18:41:48 +00:00
The ``mod_watch`` function for the service state looks like this:
2012-02-17 18:41:48 +00:00
.. code-block:: python
2012-11-24 07:07:53 +00:00
def mod_watch(name, sig=None, reload=False, full_restart=False):
2012-02-17 18:41:48 +00:00
'''
The service watcher, called to invoke the watch command.
name
The name of the init or rc script used to manage the service
sig
The string to search for when looking for the service process with ps
'''
if __salt__['service.status'](name, sig):
2012-11-24 07:07:53 +00:00
if 'service.reload' in __salt__ and reload:
restart_func = __salt__['service.reload']
elif 'service.full_restart' in __salt__ and full_restart:
restart_func = __salt__['service.full_restart']
else:
restart_func = __salt__['service.restart']
else:
restart_func = __salt__['service.start']
result = restart_func(name)
2012-02-17 18:41:48 +00:00
return {'name': name,
2012-11-24 07:07:53 +00:00
'changes': {name: result},
'result': result,
'comment': 'Service restarted' if result else \
'Failed to restart the service'
}
2012-02-17 18:41:48 +00:00
The watch requisite only works if the state that is watching has a
``mod_watch`` function written. If watch is set on a state that does not have
a ``mod_watch`` function (like pkg), then the listed states will behave only
as if they were under a ``require`` statement.
2012-02-17 18:41:48 +00:00
2012-11-24 07:07:53 +00:00
Also notice that a ``mod_watch`` may accept additional keyword arguments,
which, in the sls file, will be taken from the same set of arguments specified
for the state that includes the ``watch`` requisite. This means, for the
2013-10-24 13:40:21 +00:00
earlier ``service.running`` example above, the service can be set to
2012-11-24 07:07:53 +00:00
``reload`` instead of restart like this:
.. code-block:: yaml
redis:
# ... other state declarations omitted ...
service.running:
- enable: True
- reload: True
- watch:
- file: /etc/redis.conf
- pkg: redis
2011-11-24 23:21:08 +00:00
The Order Option
================
Before using the order option, remember that the majority of state ordering
should be done with a :term:`requisite declaration`, and that a requisite
declaration will override an order option.
2011-11-24 23:21:08 +00:00
The order option is used by adding an order number to a state declaration
with the option `order`:
.. code-block:: yaml
2011-11-26 08:02:14 +00:00
2011-11-24 23:21:08 +00:00
vim:
pkg.installed:
2011-11-24 23:21:08 +00:00
- order: 1
By adding the order option to `1` this ensures that the vim package will be
installed in tandem with any other state declaration set to the order `1`.
Any state declared without an order option will be executed after all states
with order options are executed.
But this construct can only handle ordering states from the beginning.
2013-11-19 15:57:51 +00:00
Certain circimstances will present a situation where it is desireable to send
2013-10-24 13:40:21 +00:00
a state to the end of the line. To do this, set the order to ``last``:
.. code-block:: yaml
2011-11-26 08:02:14 +00:00
vim:
pkg.installed:
- order: last
Remember that requisite statements override the order option. So the order
option should be applied to the highest component of the requisite chain:
.. code-block:: yaml
vim:
pkg.installed:
- order: last
- require:
- file: /etc/vimrc
/etc/vimrc:
file.managed:
- source: salt://edit/vimrc