2014-05-27 20:19:25 +00:00
|
|
|
|
.. _requisites:
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
===========================================
|
|
|
|
|
Requisites and Other Global State Arguments
|
|
|
|
|
===========================================
|
|
|
|
|
|
2012-05-02 19:26:24 +00:00
|
|
|
|
Requisites
|
|
|
|
|
==========
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
The Salt requisite system is used to create relationships between states. The
|
|
|
|
|
core idea being that, when one state is dependent somehow on another, that
|
2016-02-24 15:34:03 +00:00
|
|
|
|
inter-dependency can be easily defined. These dependencies are expressed by
|
2016-03-01 15:36:23 +00:00
|
|
|
|
declaring the relationships using state names and ID's or names. The
|
2016-02-24 15:34:03 +00:00
|
|
|
|
generalized form of a requisite target is ``<state name> : <ID or name>``.
|
2016-03-01 15:36:23 +00:00
|
|
|
|
The specific form is defined as a :ref:`Requisite Reference
|
2016-02-24 15:34:03 +00:00
|
|
|
|
<requisite-reference>`
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
Requisites come in two types: Direct requisites (such as ``require``),
|
|
|
|
|
and requisite_ins (such as ``require_in``). The relationships are
|
|
|
|
|
directional: a direct requisite requires something from another state.
|
|
|
|
|
However, a requisite_in inserts a requisite into the targeted state pointing to
|
2015-07-11 16:25:20 +00:00
|
|
|
|
the targeting state. The following example demonstrates a direct requisite:
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
vim:
|
2014-12-13 06:29:48 +00:00
|
|
|
|
pkg.installed: []
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
/etc/vimrc:
|
|
|
|
|
file.managed:
|
|
|
|
|
- source: salt://edit/vimrc
|
|
|
|
|
- require:
|
|
|
|
|
- pkg: vim
|
|
|
|
|
|
|
|
|
|
In the example above, the file ``/etc/vimrc`` depends on the vim package.
|
|
|
|
|
|
|
|
|
|
Requisite_in statements are the opposite. Instead of saying "I depend on
|
|
|
|
|
something", requisite_ins say "Someone depends on me":
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
vim:
|
|
|
|
|
pkg.installed:
|
|
|
|
|
- require_in:
|
|
|
|
|
- file: /etc/vimrc
|
|
|
|
|
|
|
|
|
|
/etc/vimrc:
|
|
|
|
|
file.managed:
|
|
|
|
|
- source: salt://edit/vimrc
|
|
|
|
|
|
|
|
|
|
So here, with a requisite_in, the same thing is accomplished as in the first
|
|
|
|
|
example, but the other way around. The vim package is saying "/etc/vimrc depends
|
2015-02-07 17:46:03 +00:00
|
|
|
|
on me". This will result in a ``require`` being inserted into the
|
|
|
|
|
``/etc/vimrc`` state which targets the ``vim`` state.
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
In the end, a single dependency map is created and everything is executed in a
|
|
|
|
|
finite and predictable order.
|
|
|
|
|
|
2015-12-08 19:22:05 +00:00
|
|
|
|
Requisite matching
|
|
|
|
|
------------------
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2015-12-08 19:22:05 +00:00
|
|
|
|
Requisites need two pieces of information for matching: The state module name –
|
|
|
|
|
e.g. ``pkg`` –, and the identifier – e.g. vim –, which can be either the ID (the
|
|
|
|
|
first line in the stanza) or the ``- name`` parameter.
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2015-12-08 19:22:05 +00:00
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
- require:
|
|
|
|
|
- pkg: vim
|
|
|
|
|
|
2015-12-24 12:07:44 +00:00
|
|
|
|
Omitting state module in requisites
|
|
|
|
|
-----------------------------------
|
|
|
|
|
|
2016-02-10 22:45:28 +00:00
|
|
|
|
.. versionadded:: 2016.3.0
|
2015-12-24 12:07:44 +00:00
|
|
|
|
|
2016-02-10 22:45:28 +00:00
|
|
|
|
In version 2016.3.0, the state module name was made optional. If the state module
|
2015-12-24 12:07:44 +00:00
|
|
|
|
is omitted, all states matching the ID will be required, regardless of which
|
|
|
|
|
module they are using.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
- require:
|
|
|
|
|
- vim
|
|
|
|
|
|
2015-12-08 19:22:05 +00:00
|
|
|
|
State target matching
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
In order to understand how state targets are matched, it is helpful to know
|
|
|
|
|
:ref:`how the state compiler is working <compiler_ordering>`. Consider the following
|
|
|
|
|
example:
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
Deploy server package:
|
|
|
|
|
file.managed:
|
|
|
|
|
- name: /usr/local/share/myapp.tar.xz
|
|
|
|
|
- source: salt://myapp.tar.xz
|
|
|
|
|
|
|
|
|
|
Extract server package:
|
|
|
|
|
archive.extracted:
|
|
|
|
|
- name: /usr/local/share/myapp
|
|
|
|
|
- source: /usr/local/share/myapp.tar.xz
|
|
|
|
|
- archive_format: tar
|
|
|
|
|
- onchanges:
|
|
|
|
|
- file: Deploy server package
|
|
|
|
|
|
|
|
|
|
The first formula is converted to a dictionary which looks as follows (represented
|
|
|
|
|
as YAML, some properties omitted for simplicity) as `High Data`:
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2015-12-08 19:22:05 +00:00
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
Deploy server package:
|
|
|
|
|
file:
|
|
|
|
|
- managed
|
|
|
|
|
- name: /usr/local/share/myapp.tar.xz
|
|
|
|
|
- source: salt://myapp.tar.xz
|
|
|
|
|
|
|
|
|
|
The ``file.managed`` format used in the formula is essentially syntactic sugar:
|
|
|
|
|
at the end, the target is ``file``, which is used in the ``Extract server package``
|
|
|
|
|
state above.
|
|
|
|
|
|
|
|
|
|
Identifier matching
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
Requisites match on both the ID Declaration and the ``name`` parameter.
|
|
|
|
|
This means that, in the "Deploy server package" example above, a ``require``
|
|
|
|
|
requisite would match with with ``Deploy server package`` *or* ``/usr/local/share/myapp.tar.xz``,
|
|
|
|
|
so either of the following versions for "Extract server package" works:
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
# (Archive arguments omitted for simplicity)
|
|
|
|
|
|
|
|
|
|
# Match by ID declaration
|
|
|
|
|
Extract server package:
|
|
|
|
|
archive.extracted:
|
|
|
|
|
- onchanges:
|
|
|
|
|
- file: Deploy server package
|
|
|
|
|
|
|
|
|
|
# Match by name parameter
|
|
|
|
|
Extract server package:
|
|
|
|
|
archive.extracted:
|
|
|
|
|
- onchanges:
|
|
|
|
|
- file: /usr/local/share/myapp.tar.xz
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Direct Requisite and Requisite_in types
|
|
|
|
|
---------------------------------------
|
|
|
|
|
|
2015-02-07 17:46:03 +00:00
|
|
|
|
There are several direct requisite statements that can be used in Salt:
|
|
|
|
|
|
|
|
|
|
* ``require``
|
|
|
|
|
* ``watch``
|
|
|
|
|
* ``prereq``
|
|
|
|
|
* ``use``
|
|
|
|
|
* ``onchanges``
|
|
|
|
|
* ``onfail``
|
|
|
|
|
|
|
|
|
|
Each direct requisite also has a corresponding requisite_in:
|
|
|
|
|
|
|
|
|
|
* ``require_in``
|
|
|
|
|
* ``watch_in``
|
|
|
|
|
* ``prereq_in``
|
|
|
|
|
* ``use_in``
|
|
|
|
|
* ``onchanges_in``
|
|
|
|
|
* ``onfail_in``
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
All of the requisites define specific relationships and always work with the
|
|
|
|
|
dependency logic defined above.
|
|
|
|
|
|
2014-08-05 22:50:59 +00:00
|
|
|
|
.. _requisites-require:
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
require
|
|
|
|
|
~~~~~~~
|
|
|
|
|
|
2016-08-04 19:14:39 +00:00
|
|
|
|
The use of ``require`` demands that the required state executes before the
|
|
|
|
|
dependent state. The state containing the ``require`` requisite is defined as the
|
|
|
|
|
dependent state. The state specified in the ``require`` statement is defined as the
|
|
|
|
|
required state. If the required state's execution succeeds, the dependent state
|
|
|
|
|
will then execute. If the required state's execution fails, the dependent state
|
2014-07-14 16:33:02 +00:00
|
|
|
|
will not execute. In the first example above, the file ``/etc/vimrc`` will only
|
|
|
|
|
execute after the vim package is installed successfully.
|
|
|
|
|
|
2016-05-04 16:18:19 +00:00
|
|
|
|
Require an Entire SLS File
|
2014-07-14 16:33:02 +00:00
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
As of Salt 0.16.0, it is possible to require an entire sls file. Do this first by
|
|
|
|
|
including the sls file and then setting a state to ``require`` the included sls file:
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
include:
|
|
|
|
|
- foo
|
|
|
|
|
|
|
|
|
|
bar:
|
|
|
|
|
pkg.installed:
|
|
|
|
|
- require:
|
|
|
|
|
- sls: foo
|
|
|
|
|
|
2016-05-04 16:18:19 +00:00
|
|
|
|
This will add all of the state declarations found in the given sls file. This means
|
|
|
|
|
that every state in sls `foo` will be required. This makes it very easy to batch
|
|
|
|
|
large groups of states easily in any requisite statement.
|
|
|
|
|
|
2014-08-05 22:50:59 +00:00
|
|
|
|
.. _requisites-watch:
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
watch
|
|
|
|
|
~~~~~
|
|
|
|
|
|
|
|
|
|
``watch`` statements are used to add additional behavior when there are changes
|
|
|
|
|
in other states.
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
If a state should only execute when another state has changes, and
|
|
|
|
|
otherwise do nothing, the new ``onchanges`` requisite should be used
|
2015-02-07 17:46:03 +00:00
|
|
|
|
instead of ``watch``. ``watch`` is designed to add *additional* behavior
|
2015-12-09 11:51:01 +00:00
|
|
|
|
when there are changes, but otherwise the state executes normally.
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
The state containing the ``watch`` requisite is defined as the watching
|
|
|
|
|
state. The state specified in the ``watch`` statement is defined as the watched
|
|
|
|
|
state. When the watched state executes, it will return a dictionary containing
|
|
|
|
|
a key named "changes". Here are two examples of state return dictionaries,
|
|
|
|
|
shown in json for clarity:
|
|
|
|
|
|
|
|
|
|
.. code-block:: json
|
2016-01-20 04:57:45 +00:00
|
|
|
|
|
2015-12-09 11:51:01 +00:00
|
|
|
|
{
|
|
|
|
|
"local": {
|
|
|
|
|
"file_|-/tmp/foo_|-/tmp/foo_|-directory": {
|
|
|
|
|
"comment": "Directory /tmp/foo updated",
|
|
|
|
|
"__run_num__": 0,
|
|
|
|
|
"changes": {
|
|
|
|
|
"user": "bar"
|
|
|
|
|
},
|
|
|
|
|
"name": "/tmp/foo",
|
|
|
|
|
"result": true
|
|
|
|
|
}
|
2014-07-14 16:33:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-09 11:51:01 +00:00
|
|
|
|
{
|
|
|
|
|
"local": {
|
|
|
|
|
"pkgrepo_|-salt-minion_|-salt-minion_|-managed": {
|
|
|
|
|
"comment": "Package repo 'salt-minion' already configured",
|
|
|
|
|
"__run_num__": 0,
|
|
|
|
|
"changes": {},
|
|
|
|
|
"name": "salt-minion",
|
|
|
|
|
"result": true
|
|
|
|
|
}
|
2014-07-14 16:33:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
If the "result" of the watched state is ``True``, the watching state *will
|
2015-12-09 11:51:01 +00:00
|
|
|
|
execute normally*, and if it is ``False``, the watching state will never run.
|
|
|
|
|
This part of ``watch`` mirrors the functionality of the ``require`` requisite.
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2015-12-09 11:51:01 +00:00
|
|
|
|
If the "result" of the watched state is ``True`` *and* the "changes"
|
2014-07-14 16:33:02 +00:00
|
|
|
|
key contains a populated dictionary (changes occurred in the watched state),
|
2015-02-07 17:46:03 +00:00
|
|
|
|
then the ``watch`` requisite can add additional behavior. This additional
|
2014-07-14 16:33:02 +00:00
|
|
|
|
behavior is defined by the ``mod_watch`` function within the watching state
|
2015-02-07 17:46:03 +00:00
|
|
|
|
module. If the ``mod_watch`` function exists in the watching state module, it
|
|
|
|
|
will be called *in addition to* the normal watching state. The return data
|
2014-07-14 16:33:02 +00:00
|
|
|
|
from the ``mod_watch`` function is what will be returned to the master in this
|
|
|
|
|
case; the return data from the main watching function is discarded.
|
|
|
|
|
|
|
|
|
|
If the "changes" key contains an empty dictionary, the ``watch`` requisite acts
|
|
|
|
|
exactly like the ``require`` requisite (the watching state will execute if
|
|
|
|
|
"result" is ``True``, and fail if "result" is ``False`` in the watched state).
|
|
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
|
|
Not all state modules contain ``mod_watch``. If ``mod_watch`` is absent
|
|
|
|
|
from the watching state module, the ``watch`` requisite behaves exactly
|
|
|
|
|
like a ``require`` requisite.
|
|
|
|
|
|
|
|
|
|
A good example of using ``watch`` is with a :mod:`service.running
|
|
|
|
|
<salt.states.service.running>` state. When a service watches a state, then
|
|
|
|
|
the service is reloaded/restarted when the watched state changes, in addition
|
|
|
|
|
to Salt ensuring that the service is running.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
ntpd:
|
|
|
|
|
service.running:
|
|
|
|
|
- watch:
|
|
|
|
|
- file: /etc/ntp.conf
|
|
|
|
|
file.managed:
|
|
|
|
|
- name: /etc/ntp.conf
|
|
|
|
|
- source: salt://ntp/files/ntp.conf
|
|
|
|
|
|
2014-08-05 22:50:59 +00:00
|
|
|
|
.. _requisites-prereq:
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
prereq
|
|
|
|
|
~~~~~~
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 0.16.0
|
|
|
|
|
|
|
|
|
|
``prereq`` allows for actions to be taken based on the expected results of
|
|
|
|
|
a state that has not yet been executed. The state containing the ``prereq``
|
|
|
|
|
requisite is defined as the pre-requiring state. The state specified in the
|
|
|
|
|
``prereq`` statement is defined as the pre-required state.
|
|
|
|
|
|
2014-11-04 18:36:30 +00:00
|
|
|
|
When a ``prereq`` requisite is evaluated, the pre-required state reports if it
|
|
|
|
|
expects to have any changes. It does this by running the pre-required single
|
|
|
|
|
state as a test-run by enabling ``test=True``. This test-run will return a
|
|
|
|
|
dictionary containing a key named "changes". (See the ``watch`` section above
|
|
|
|
|
for examples of "changes" dictionaries.)
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
If the "changes" key contains a populated dictionary, it means that the
|
|
|
|
|
pre-required state expects changes to occur when the state is actually
|
2014-10-27 16:59:07 +00:00
|
|
|
|
executed, as opposed to the test-run. The pre-requiring state will now
|
|
|
|
|
actually run. If the pre-requiring state executes successfully, the
|
|
|
|
|
pre-required state will then execute. If the pre-requiring state fails, the
|
|
|
|
|
pre-required state will not execute.
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
If the "changes" key contains an empty dictionary, this means that changes are
|
|
|
|
|
not expected by the pre-required state. Neither the pre-required state nor the
|
|
|
|
|
pre-requiring state will run.
|
|
|
|
|
|
|
|
|
|
The best way to define how ``prereq`` operates is displayed in the following
|
|
|
|
|
practical example: When a service should be shut down because underlying code
|
|
|
|
|
is going to change, the service should be off-line while the update occurs. In
|
|
|
|
|
this example, ``graceful-down`` is the pre-requiring state and ``site-code``
|
|
|
|
|
is the pre-required state.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
graceful-down:
|
|
|
|
|
cmd.run:
|
|
|
|
|
- name: service apache graceful
|
|
|
|
|
- prereq:
|
|
|
|
|
- file: site-code
|
|
|
|
|
|
|
|
|
|
site-code:
|
|
|
|
|
file.recurse:
|
|
|
|
|
- name: /opt/site_code
|
|
|
|
|
- source: salt://site/code
|
|
|
|
|
|
|
|
|
|
In this case the apache server will only be shutdown if the site-code state
|
|
|
|
|
expects to deploy fresh code via the file.recurse call. The site-code
|
|
|
|
|
deployment will only be executed if the graceful-down run completes
|
|
|
|
|
successfully.
|
|
|
|
|
|
2016-12-15 21:03:56 +00:00
|
|
|
|
.. _requisites-onfail:
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
onfail
|
|
|
|
|
~~~~~~
|
|
|
|
|
|
2014-07-15 22:53:29 +00:00
|
|
|
|
.. versionadded:: 2014.7.0
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
The ``onfail`` requisite allows for reactions to happen strictly as a response
|
|
|
|
|
to the failure of another state. This can be used in a number of ways, such as
|
|
|
|
|
executing a second attempt to set up a service or begin to execute a separate
|
|
|
|
|
thread of states because of a failure.
|
|
|
|
|
|
|
|
|
|
The ``onfail`` requisite is applied in the same way as ``require`` as ``watch``:
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
primary_mount:
|
2014-12-13 06:29:48 +00:00
|
|
|
|
mount.mounted:
|
2014-07-14 16:33:02 +00:00
|
|
|
|
- name: /mnt/share
|
|
|
|
|
- device: 10.0.0.45:/share
|
|
|
|
|
- fstype: nfs
|
|
|
|
|
|
|
|
|
|
backup_mount:
|
2014-12-13 06:29:48 +00:00
|
|
|
|
mount.mounted:
|
2014-07-14 16:33:02 +00:00
|
|
|
|
- name: /mnt/share
|
|
|
|
|
- device: 192.168.40.34:/share
|
|
|
|
|
- fstype: nfs
|
|
|
|
|
- onfail:
|
|
|
|
|
- mount: primary_mount
|
|
|
|
|
|
2016-02-16 17:44:42 +00:00
|
|
|
|
.. note::
|
|
|
|
|
|
2016-10-27 21:26:30 +00:00
|
|
|
|
Beginning in the ``2016.11.0`` release of Salt, ``onfail`` uses OR logic for
|
|
|
|
|
multiple listed ``onfail`` requisites. Prior to the ``2016.11.0`` release,
|
2016-02-16 17:44:42 +00:00
|
|
|
|
``onfail`` used AND logic. See `Issue #22370`_ for more information.
|
|
|
|
|
|
|
|
|
|
.. _Issue #22370: https://github.com/saltstack/salt/issues/22370
|
|
|
|
|
|
2016-12-15 21:03:56 +00:00
|
|
|
|
.. _requisites-onchanges:
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
onchanges
|
|
|
|
|
~~~~~~~~~
|
|
|
|
|
|
2014-07-15 22:53:29 +00:00
|
|
|
|
.. versionadded:: 2014.7.0
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
The ``onchanges`` requisite makes a state only apply if the required states
|
|
|
|
|
generate changes, and if the watched state's "result" is ``True``. This can be
|
|
|
|
|
a useful way to execute a post hook after changing aspects of a system.
|
|
|
|
|
|
2015-06-16 14:02:11 +00:00
|
|
|
|
If a state has multiple ``onchanges`` requisites then the state will trigger
|
|
|
|
|
if any of the watched states changes.
|
|
|
|
|
|
2016-09-30 04:02:39 +00:00
|
|
|
|
.. note::
|
|
|
|
|
One easy-to-make mistake is to use ``onchanges_in`` when ``onchanges`` is
|
|
|
|
|
supposed to be used. For example, the below configuration is not correct:
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
myservice:
|
|
|
|
|
pkg.installed:
|
|
|
|
|
- name: myservice
|
|
|
|
|
file.managed:
|
|
|
|
|
- name: /etc/myservice/myservice.conf
|
|
|
|
|
- source: salt://myservice/files/myservice.conf
|
|
|
|
|
- mode: 600
|
|
|
|
|
cmd.run:
|
|
|
|
|
- name: /usr/libexec/myservice/post-changes-hook.sh
|
|
|
|
|
- onchanges_in:
|
|
|
|
|
- file: /etc/myservice/myservice.conf
|
|
|
|
|
|
|
|
|
|
This will set up a requisite relationship in which the ``cmd.run`` state
|
|
|
|
|
always executes, and the ``file.managed`` state only executes if the
|
|
|
|
|
``cmd.run`` state has changes (which it always will, since the ``cmd.run``
|
|
|
|
|
state includes the command results as changes).
|
|
|
|
|
|
|
|
|
|
It may semantically seem like the the ``cmd.run`` state should only run
|
|
|
|
|
when there are changes in the file state, but remember that requisite
|
|
|
|
|
relationships involve one state watching another state, and a
|
|
|
|
|
:ref:`requisite_in <requisites-onchanges-in>` does the opposite: it forces
|
|
|
|
|
the specified state to watch the state with the ``requisite_in``.
|
|
|
|
|
|
|
|
|
|
The correct usage would be:
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
myservice:
|
|
|
|
|
pkg.installed:
|
|
|
|
|
- name: myservice
|
|
|
|
|
file.managed:
|
|
|
|
|
- name: /etc/myservice/myservice.conf
|
|
|
|
|
- source: salt://myservice/files/myservice.conf
|
|
|
|
|
- mode: 600
|
|
|
|
|
cmd.run:
|
|
|
|
|
- name: /usr/libexec/myservice/post-changes-hook.sh
|
|
|
|
|
- onchanges:
|
|
|
|
|
- file: /etc/myservice/myservice.conf
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
use
|
|
|
|
|
~~~
|
|
|
|
|
|
|
|
|
|
The ``use`` requisite is used to inherit the arguments passed in another
|
|
|
|
|
id declaration. This is useful when many files need to have the same defaults.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
/etc/foo.conf:
|
|
|
|
|
file.managed:
|
|
|
|
|
- source: salt://foo.conf
|
|
|
|
|
- template: jinja
|
|
|
|
|
- mkdirs: True
|
|
|
|
|
- user: apache
|
|
|
|
|
- group: apache
|
|
|
|
|
- mode: 755
|
|
|
|
|
|
|
|
|
|
/etc/bar.conf
|
|
|
|
|
file.managed:
|
|
|
|
|
- source: salt://bar.conf
|
|
|
|
|
- use:
|
|
|
|
|
- file: /etc/foo.conf
|
|
|
|
|
|
|
|
|
|
The ``use`` statement was developed primarily for the networking states but
|
|
|
|
|
can be used on any states in Salt. This makes sense for the networking state
|
|
|
|
|
because it can define a long list of options that need to be applied to
|
|
|
|
|
multiple network interfaces.
|
|
|
|
|
|
|
|
|
|
The ``use`` statement does not inherit the requisites arguments of the
|
|
|
|
|
targeted state. This means also a chain of ``use`` requisites would not
|
|
|
|
|
inherit inherited options.
|
|
|
|
|
|
|
|
|
|
.. _requisites-require-in:
|
|
|
|
|
.. _requisites-watch-in:
|
2016-09-30 04:02:39 +00:00
|
|
|
|
.. _requisites-onchanges-in:
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
The _in versions of requisites
|
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
All of the requisites also have corresponding requisite_in versions, which do
|
2015-02-07 17:46:03 +00:00
|
|
|
|
the reverse of their normal counterparts. The examples below all use
|
2014-07-14 16:33:02 +00:00
|
|
|
|
``require_in`` as the example, but note that all of the ``_in`` requisites work
|
2015-02-07 17:46:03 +00:00
|
|
|
|
the same way: They result in a normal requisite in the targeted state, which
|
|
|
|
|
targets the state which has defines the requisite_in. Thus, a ``require_in``
|
|
|
|
|
causes the target state to ``require`` the targeting state. Similarly, a
|
|
|
|
|
``watch_in`` causes the target state to ``watch`` the targeting state. This
|
2014-07-14 16:33:02 +00:00
|
|
|
|
pattern continues for the rest of the requisites.
|
|
|
|
|
|
|
|
|
|
If a state declaration needs to be required by another state declaration then
|
|
|
|
|
``require_in`` can accommodate it. Therefore, these two sls files would be the
|
|
|
|
|
same in the end:
|
|
|
|
|
|
|
|
|
|
Using ``require``
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
httpd:
|
2014-12-13 06:29:48 +00:00
|
|
|
|
pkg.installed: []
|
|
|
|
|
service.running:
|
2014-07-14 16:33:02 +00:00
|
|
|
|
- require:
|
|
|
|
|
- pkg: httpd
|
|
|
|
|
|
|
|
|
|
Using ``require_in``
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
httpd:
|
2014-12-13 06:29:48 +00:00
|
|
|
|
pkg.installed:
|
2014-07-14 16:33:02 +00:00
|
|
|
|
- require_in:
|
|
|
|
|
- service: httpd
|
2014-12-13 06:29:48 +00:00
|
|
|
|
service.running: []
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
|
|
|
|
The ``require_in`` statement is particularly useful when assigning a require
|
|
|
|
|
in a separate sls file. For instance it may be common for httpd to require
|
|
|
|
|
components used to set up PHP or mod_python, but the HTTP state does not need
|
|
|
|
|
to be aware of the additional components that require it when it is set up:
|
|
|
|
|
|
|
|
|
|
http.sls
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
httpd:
|
2014-12-13 06:29:48 +00:00
|
|
|
|
pkg.installed: []
|
|
|
|
|
service.running:
|
2014-07-14 16:33:02 +00:00
|
|
|
|
- require:
|
|
|
|
|
- pkg: httpd
|
|
|
|
|
|
|
|
|
|
php.sls
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
include:
|
|
|
|
|
- http
|
|
|
|
|
|
|
|
|
|
php:
|
2014-12-13 06:29:48 +00:00
|
|
|
|
pkg.installed:
|
2014-07-14 16:33:02 +00:00
|
|
|
|
- require_in:
|
|
|
|
|
- service: httpd
|
|
|
|
|
|
|
|
|
|
mod_python.sls
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
include:
|
|
|
|
|
- http
|
|
|
|
|
|
|
|
|
|
mod_python:
|
2014-12-13 06:29:48 +00:00
|
|
|
|
pkg.installed:
|
2014-07-14 16:33:02 +00:00
|
|
|
|
- require_in:
|
|
|
|
|
- service: httpd
|
|
|
|
|
|
|
|
|
|
Now the httpd server will only start if php or mod_python are first verified to
|
|
|
|
|
be installed. Thus allowing for a requisite to be defined "after the fact".
|
|
|
|
|
|
|
|
|
|
|
2016-03-23 16:15:29 +00:00
|
|
|
|
.. _requisites-fire-event:
|
|
|
|
|
|
|
|
|
|
Fire Event Notifications
|
|
|
|
|
========================
|
|
|
|
|
|
|
|
|
|
.. versionadded:: 2015.8.0
|
|
|
|
|
|
|
|
|
|
The `fire_event` option in a state will cause the minion to send an event to
|
|
|
|
|
the Salt Master upon completion of that individual state.
|
|
|
|
|
|
|
|
|
|
The following example will cause the minion to send an event to the Salt Master
|
|
|
|
|
with a tag of `salt/state_result/20150505121517276431/dasalt/nano` and the
|
|
|
|
|
result of the state will be the data field of the event. Notice that the `name`
|
|
|
|
|
of the state gets added to the tag.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
nano_stuff:
|
|
|
|
|
pkg.installed:
|
|
|
|
|
- name: nano
|
|
|
|
|
- fire_event: True
|
|
|
|
|
|
|
|
|
|
In the following example instead of setting `fire_event` to `True`,
|
|
|
|
|
`fire_event` is set to an arbitrary string, which will cause the event to be
|
|
|
|
|
sent with this tag:
|
|
|
|
|
`salt/state_result/20150505121725642845/dasalt/custom/tag/nano/finished`
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
nano_stuff:
|
|
|
|
|
pkg.installed:
|
|
|
|
|
- name: nano
|
|
|
|
|
- fire_event: custom/tag/nano/finished
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
Altering States
|
|
|
|
|
===============
|
|
|
|
|
|
|
|
|
|
The state altering system is used to make sure that states are evaluated exactly
|
2015-02-07 17:46:03 +00:00
|
|
|
|
as the user expects. It can be used to double check that a state preformed
|
2014-07-14 16:33:02 +00:00
|
|
|
|
exactly how it was expected to, or to make 100% sure that a state only runs
|
2015-02-07 17:46:03 +00:00
|
|
|
|
under certain conditions. The use of unless or onlyif options help make states
|
2015-10-20 19:44:06 +00:00
|
|
|
|
even more stateful. The ``check_cmd`` option helps ensure that the result of a
|
2014-07-14 16:33:02 +00:00
|
|
|
|
state is evaluated correctly.
|
|
|
|
|
|
2016-01-07 21:08:30 +00:00
|
|
|
|
Reload
|
|
|
|
|
------
|
|
|
|
|
|
|
|
|
|
``reload_modules`` is a boolean option that forces salt to reload its modules
|
2016-08-15 15:53:56 +00:00
|
|
|
|
after a state finishes. ``reload_pillar`` and ``reload_grains`` can also be set.
|
|
|
|
|
See :ref:`Reloading Modules <reloading-modules>`.
|
2016-01-07 21:08:30 +00:00
|
|
|
|
|
2016-10-06 21:58:29 +00:00
|
|
|
|
.. _unless-requisite:
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
Unless
|
2014-08-11 16:16:01 +00:00
|
|
|
|
------
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2014-07-15 22:53:29 +00:00
|
|
|
|
.. versionadded:: 2014.7.0
|
|
|
|
|
|
2014-09-22 22:03:04 +00:00
|
|
|
|
The ``unless`` requisite specifies that a state should only run when any of
|
|
|
|
|
the specified commands return ``False``. The ``unless`` requisite operates
|
2015-12-15 22:45:19 +00:00
|
|
|
|
as NAND and is useful in giving more granular control over when a state should
|
2014-09-22 22:03:04 +00:00
|
|
|
|
execute.
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2015-07-11 16:25:20 +00:00
|
|
|
|
**NOTE**: Under the hood ``unless`` calls ``cmd.retcode`` with
|
2015-12-15 23:14:31 +00:00
|
|
|
|
``python_shell=True``. This means the commands referenced by ``unless`` will be
|
2015-04-06 20:20:08 +00:00
|
|
|
|
parsed by a shell, so beware of side-effects as this shell will be run with the
|
2015-12-15 23:14:31 +00:00
|
|
|
|
same privileges as the salt-minion. Also be aware that the boolean value is
|
|
|
|
|
determined by the shell's concept of ``True`` and ``False``, rather than Python's
|
|
|
|
|
concept of ``True`` and ``False``.
|
2015-04-06 20:20:08 +00:00
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
vim:
|
|
|
|
|
pkg.installed:
|
|
|
|
|
- unless:
|
2014-09-17 23:00:18 +00:00
|
|
|
|
- rpm -q vim-enhanced
|
|
|
|
|
- ls /usr/bin/vim
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2014-09-22 22:03:04 +00:00
|
|
|
|
In the example above, the state will only run if either the vim-enhanced
|
|
|
|
|
package is not installed (returns ``False``) or if /usr/bin/vim does not
|
|
|
|
|
exist (returns ``False``). The state will run if both commands return
|
|
|
|
|
``False``.
|
|
|
|
|
|
|
|
|
|
However, the state will not run if both commands return ``True``.
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2015-04-16 17:49:35 +00:00
|
|
|
|
Unless checks are resolved for each name to which they are associated.
|
2014-11-26 16:17:14 +00:00
|
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
deploy_app:
|
|
|
|
|
cmd.run:
|
2015-03-24 19:02:27 +00:00
|
|
|
|
- names:
|
2015-04-16 17:49:35 +00:00
|
|
|
|
- first_deploy_cmd
|
|
|
|
|
- second_deploy_cmd
|
2015-07-11 16:25:20 +00:00
|
|
|
|
- unless: ls /usr/bin/vim
|
2014-11-26 16:17:14 +00:00
|
|
|
|
|
|
|
|
|
In the above case, ``some_check`` will be run prior to _each_ name -- once for
|
|
|
|
|
``first_deploy_cmd`` and a second time for ``second_deploy_cmd``.
|
|
|
|
|
|
2016-10-06 21:58:29 +00:00
|
|
|
|
.. _onlyif-requisite:
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
Onlyif
|
2014-08-11 16:16:01 +00:00
|
|
|
|
------
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2014-07-15 22:53:29 +00:00
|
|
|
|
.. versionadded:: 2014.7.0
|
|
|
|
|
|
2015-12-15 22:42:56 +00:00
|
|
|
|
The ``onlyif`` requisite specifies that if each command listed in ``onlyif``
|
2015-12-15 22:45:19 +00:00
|
|
|
|
returns ``True``, then the state is run. If any of the specified commands
|
2014-09-22 22:03:04 +00:00
|
|
|
|
return ``False``, the state will not run.
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2015-07-11 16:25:20 +00:00
|
|
|
|
**NOTE**: Under the hood ``onlyif`` calls ``cmd.retcode`` with
|
2015-12-15 23:14:31 +00:00
|
|
|
|
``python_shell=True``. This means the commands referenced by ``onlyif`` will be
|
2015-04-06 20:20:08 +00:00
|
|
|
|
parsed by a shell, so beware of side-effects as this shell will be run with the
|
2015-12-15 23:14:31 +00:00
|
|
|
|
same privileges as the salt-minion. Also be aware that the boolean value is
|
|
|
|
|
determined by the shell's concept of ``True`` and ``False``, rather than Python's
|
|
|
|
|
concept of ``True`` and ``False``.
|
2015-04-06 20:20:08 +00:00
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
stop-volume:
|
|
|
|
|
module.run:
|
|
|
|
|
- name: glusterfs.stop_volume
|
|
|
|
|
- m_name: work
|
|
|
|
|
- onlyif:
|
2014-09-18 19:01:38 +00:00
|
|
|
|
- gluster volume status work
|
2014-07-14 16:33:02 +00:00
|
|
|
|
- order: 1
|
|
|
|
|
|
|
|
|
|
remove-volume:
|
|
|
|
|
module.run:
|
|
|
|
|
- name: glusterfs.delete
|
|
|
|
|
- m_name: work
|
|
|
|
|
- onlyif:
|
2014-09-18 19:01:38 +00:00
|
|
|
|
- gluster volume info work
|
2014-07-14 16:33:02 +00:00
|
|
|
|
- watch:
|
|
|
|
|
- cmd: stop-volume
|
|
|
|
|
|
2014-09-22 22:03:04 +00:00
|
|
|
|
The above example ensures that the stop_volume and delete modules only run
|
|
|
|
|
if the gluster commands return a 0 ret value.
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2014-07-24 18:19:38 +00:00
|
|
|
|
Listen/Listen_in
|
2014-08-11 16:16:01 +00:00
|
|
|
|
----------------
|
2014-07-24 18:19:38 +00:00
|
|
|
|
|
|
|
|
|
.. versionadded:: 2014.7.0
|
|
|
|
|
|
|
|
|
|
listen and its counterpart listen_in trigger mod_wait functions for states,
|
|
|
|
|
when those states succeed and result in changes, similar to how watch its
|
2014-12-12 19:34:51 +00:00
|
|
|
|
counterpart watch_in. Unlike watch and watch_in, listen, and listen_in will
|
2014-07-24 18:19:38 +00:00
|
|
|
|
not modify the order of states and can be used to ensure your states are
|
|
|
|
|
executed in the order they are defined. All listen/listen_in actions will occur
|
|
|
|
|
at the end of a state run, after all states have completed.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
restart-apache2:
|
2014-08-05 01:32:38 +00:00
|
|
|
|
service.running:
|
|
|
|
|
- name: apache2
|
|
|
|
|
- listen:
|
2014-09-18 19:01:38 +00:00
|
|
|
|
- file: /etc/apache2/apache2.conf
|
2014-07-24 18:19:38 +00:00
|
|
|
|
|
|
|
|
|
configure-apache2:
|
2014-08-05 01:32:38 +00:00
|
|
|
|
file.managed:
|
2015-06-07 11:14:26 +00:00
|
|
|
|
- name: /etc/apache2/apache2.conf
|
2014-08-05 01:32:38 +00:00
|
|
|
|
- source: salt://apache2/apache2.conf
|
2014-07-24 18:19:38 +00:00
|
|
|
|
|
|
|
|
|
This example will cause apache2 to be restarted when the apache2.conf file is
|
|
|
|
|
changed, but the apache2 restart will happen at the end of the state run.
|
|
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
restart-apache2:
|
2014-08-05 01:32:38 +00:00
|
|
|
|
service.running:
|
|
|
|
|
- name: apache2
|
2014-07-24 18:19:38 +00:00
|
|
|
|
|
|
|
|
|
configure-apache2:
|
2014-08-05 01:32:38 +00:00
|
|
|
|
file.managed:
|
2015-06-07 11:14:26 +00:00
|
|
|
|
- name: /etc/apache2/apache2.conf
|
2014-08-05 01:32:38 +00:00
|
|
|
|
- source: salt://apache2/apache2.conf
|
|
|
|
|
- listen_in:
|
2014-09-18 19:01:38 +00:00
|
|
|
|
- service: apache2
|
2014-07-24 18:19:38 +00:00
|
|
|
|
|
|
|
|
|
This example does the same as the above example, but puts the state argument
|
|
|
|
|
on the file resource, rather than the service resource.
|
|
|
|
|
|
2014-09-17 23:00:18 +00:00
|
|
|
|
check_cmd
|
2014-08-11 16:16:01 +00:00
|
|
|
|
---------
|
2014-07-14 16:33:02 +00:00
|
|
|
|
|
2014-07-15 22:53:29 +00:00
|
|
|
|
.. versionadded:: 2014.7.0
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
Check Command is used for determining that a state did or did not run as
|
|
|
|
|
expected.
|
|
|
|
|
|
2015-07-11 16:25:20 +00:00
|
|
|
|
**NOTE**: Under the hood ``check_cmd`` calls ``cmd.retcode`` with
|
|
|
|
|
``python_shell=True``. This means the commands referenced by unless will be
|
2015-04-06 20:20:08 +00:00
|
|
|
|
parsed by a shell, so beware of side-effects as this shell will be run with the
|
|
|
|
|
same privileges as the salt-minion.
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
|
|
comment-repo:
|
|
|
|
|
file.replace:
|
2015-06-07 11:14:26 +00:00
|
|
|
|
- name: /etc/yum.repos.d/fedora.repo
|
2014-07-14 16:33:02 +00:00
|
|
|
|
- pattern: ^enabled=0
|
|
|
|
|
- repl: enabled=1
|
|
|
|
|
- check_cmd:
|
2015-10-20 19:44:06 +00:00
|
|
|
|
- ! grep 'enabled=0' /etc/yum.repos.d/fedora.repo
|
|
|
|
|
|
|
|
|
|
This will attempt to do a replace on all ``enabled=0`` in the .repo file, and
|
|
|
|
|
replace them with ``enabled=1``. The ``check_cmd`` is just a bash command. It
|
|
|
|
|
will do a grep for ``enabled=0`` in the file, and if it finds any, it will
|
|
|
|
|
return a 0, which will be inverted by the leading ``!``, causing ``check_cmd``
|
|
|
|
|
to set the state as failed. If it returns a 1, meaning it didn't find any
|
|
|
|
|
``enabled=0``, it will be inverted by the leading ``!``, returning a 0, and
|
2014-07-14 16:33:02 +00:00
|
|
|
|
declaring the function succeeded.
|
|
|
|
|
|
2015-10-20 19:44:06 +00:00
|
|
|
|
**NOTE**: This requisite ``check_cmd`` functions differently than the ``check_cmd``
|
|
|
|
|
of the ``file.managed`` state.
|
|
|
|
|
|
2014-07-14 16:33:02 +00:00
|
|
|
|
Overriding Checks
|
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
|
|
There are two commands used for the above checks.
|
|
|
|
|
|
2015-02-07 17:46:03 +00:00
|
|
|
|
``mod_run_check`` is used to check for ``onlyif`` and ``unless``. If the goal is to
|
2014-08-11 16:16:01 +00:00
|
|
|
|
override the global check for these to variables, include a ``mod_run_check`` in the
|
2014-07-14 16:33:02 +00:00
|
|
|
|
salt/states/ file.
|
|
|
|
|
|
2015-02-07 17:46:03 +00:00
|
|
|
|
``mod_run_check_cmd`` is used to check for the check_cmd options. To override
|
2014-08-11 16:16:01 +00:00
|
|
|
|
this one, include a ``mod_run_check_cmd`` in the states file for the state.
|