2011-10-30 16:21:11 +00:00
|
|
|
=======================
|
|
|
|
States tutorial, part 3
|
|
|
|
=======================
|
|
|
|
|
2012-08-03 06:23:39 +00:00
|
|
|
.. note::
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2013-07-26 01:41:21 +00:00
|
|
|
This tutorial builds on topics covered in :doc:`part 1 <states_pt1>` and
|
2012-08-03 06:23:39 +00:00
|
|
|
:doc:`part 2 <states_pt2>`. It is recommended that you begin there.
|
|
|
|
|
|
|
|
This part of the tutorial will cover more advanced templating and
|
|
|
|
configuration techniques for ``sls`` files.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-10-31 01:23:12 +00:00
|
|
|
Templating SLS modules
|
|
|
|
======================
|
|
|
|
|
2012-08-03 06:23:39 +00:00
|
|
|
SLS modules may require programming logic or inline execution. This is
|
2011-10-31 01:23:12 +00:00
|
|
|
accomplished with module templating. The default module templating system used
|
|
|
|
is `Jinja2`_ and may be configured by changing the :conf_master:`renderer`
|
|
|
|
value in the master config.
|
|
|
|
|
|
|
|
.. _`Jinja2`: http://jinja.pocoo.org/
|
|
|
|
|
2012-08-03 06:23:39 +00:00
|
|
|
All states are passed through a templating system when they are initially read.
|
2012-10-02 15:57:06 +00:00
|
|
|
To make use of the templating system, simply add some templating markup.
|
2012-08-03 06:23:39 +00:00
|
|
|
An example of an sls module with templating markup may look like this:
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
.. code-block:: jinja
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2013-10-10 01:27:03 +00:00
|
|
|
{% for usr in ['moe','larry','curly'] %}
|
2011-10-30 16:21:11 +00:00
|
|
|
{{ usr }}:
|
2012-04-04 05:52:32 +00:00
|
|
|
user.present
|
2011-10-30 16:21:11 +00:00
|
|
|
{% endfor %}
|
|
|
|
|
2011-10-31 01:23:12 +00:00
|
|
|
This templated sls file once generated will look like this:
|
2011-10-30 16:21:11 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
moe:
|
2012-04-04 05:52:32 +00:00
|
|
|
user.present
|
2011-10-30 16:21:11 +00:00
|
|
|
larry:
|
2012-04-04 05:52:32 +00:00
|
|
|
user.present
|
2012-04-16 05:31:59 +00:00
|
|
|
curly:
|
2012-04-04 05:52:32 +00:00
|
|
|
user.present
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2013-07-15 21:44:17 +00:00
|
|
|
Here's a more complex example:
|
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
.. code-blocK:: jinja
|
2013-07-15 21:44:17 +00:00
|
|
|
|
|
|
|
{% for usr in 'moe','larry','curly' %}
|
|
|
|
{{ usr }}:
|
|
|
|
group:
|
|
|
|
- present
|
|
|
|
user:
|
|
|
|
- present
|
|
|
|
- gid_from_name: True
|
|
|
|
- require:
|
|
|
|
- group: {{ usr }}
|
|
|
|
{% endfor %}
|
|
|
|
|
2011-10-31 01:23:12 +00:00
|
|
|
Using Grains in SLS modules
|
|
|
|
===========================
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-10-31 01:23:12 +00:00
|
|
|
Often times a state will need to behave differently on different systems.
|
2012-08-03 06:23:39 +00:00
|
|
|
:doc:`Salt grains </topics/targeting/grains>` objects are made available
|
|
|
|
in the template context. The `grains` can be used from within sls modules:
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
.. code-block:: jinja
|
2011-10-30 16:21:11 +00:00
|
|
|
|
|
|
|
apache:
|
2012-04-04 05:52:32 +00:00
|
|
|
pkg.installed:
|
2011-10-30 16:21:11 +00:00
|
|
|
{% if grains['os'] == 'RedHat' %}
|
|
|
|
- name: httpd
|
2011-10-31 01:23:12 +00:00
|
|
|
{% elif grains['os'] == 'Ubuntu' %}
|
|
|
|
- name: apache2
|
2011-10-30 16:21:11 +00:00
|
|
|
{% endif %}
|
|
|
|
|
2011-10-31 01:23:12 +00:00
|
|
|
Calling Salt modules from templates
|
|
|
|
===================================
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2012-03-09 04:05:52 +00:00
|
|
|
All of the Salt modules loaded by the minion are available within the
|
|
|
|
templating system. This allows data to be gathered in real time on the target
|
2011-10-30 16:21:11 +00:00
|
|
|
system. It also allows for shell commands to be run easily from within the sls
|
|
|
|
modules.
|
|
|
|
|
2011-10-31 01:23:12 +00:00
|
|
|
The Salt module functions are also made available in the template context as
|
2013-08-12 03:17:47 +00:00
|
|
|
``salt:``
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
.. code-block:: jinja
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
moe:
|
2011-10-30 16:21:11 +00:00
|
|
|
user:
|
|
|
|
- present
|
2013-07-15 21:44:17 +00:00
|
|
|
- gid: {{ salt['file.group_to_gid']('some_group_that_exists') }}
|
|
|
|
|
|
|
|
Note that for the above example to work, ``some_group_that_exists`` must exist
|
|
|
|
before the state file is processed by the templating engine.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2013-07-23 02:52:52 +00:00
|
|
|
Below is an example that uses the ``network.hw_addr`` function to retrieve the
|
2013-08-12 03:17:47 +00:00
|
|
|
MAC address for eth0:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2013-07-23 02:52:52 +00:00
|
|
|
salt['network.hw_addr']('eth0')
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
Advanced SLS module syntax
|
|
|
|
==========================
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2012-08-03 06:23:39 +00:00
|
|
|
Lastly, we will cover some incredibly useful techniques for more complex State
|
2011-10-31 01:23:12 +00:00
|
|
|
trees.
|
2011-10-30 16:21:11 +00:00
|
|
|
|
2011-10-31 01:23:12 +00:00
|
|
|
:term:`Include declaration`
|
|
|
|
---------------------------
|
|
|
|
|
2012-08-03 06:23:39 +00:00
|
|
|
A previous example showed how to spread a Salt tree across several files.
|
2013-08-04 04:35:50 +00:00
|
|
|
Similarly, :doc:`requisites </ref/states/requisites>` span multiple files by
|
|
|
|
using an :term:`include declaration`. For example:
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
``python/python-libs.sls:``
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
python-dateutil:
|
2012-04-04 05:52:32 +00:00
|
|
|
pkg.installed
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
``python/django.sls:``
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
include:
|
2013-04-13 16:35:40 +00:00
|
|
|
- python.python-libs
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
django:
|
2012-04-04 05:52:32 +00:00
|
|
|
pkg.installed:
|
2011-10-31 01:23:12 +00:00
|
|
|
- require:
|
|
|
|
- pkg: python-dateutil
|
|
|
|
|
|
|
|
:term:`Extend declaration`
|
|
|
|
--------------------------
|
|
|
|
|
2012-03-09 04:05:52 +00:00
|
|
|
You can modify previous declarations by using an :term:`extend declaration`. For
|
2011-10-31 01:23:12 +00:00
|
|
|
example the following modifies the Apache tree to also restart Apache when the
|
|
|
|
vhosts file is changed:
|
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
``apache/apache.sls:``
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
apache:
|
2012-04-04 05:52:32 +00:00
|
|
|
pkg.installed
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
``apache/mywebsite.sls:``
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
include:
|
2013-02-18 08:47:00 +00:00
|
|
|
- apache.apache
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
extend:
|
2012-01-26 04:15:01 +00:00
|
|
|
apache:
|
2011-10-31 01:23:12 +00:00
|
|
|
service:
|
2013-02-18 08:47:00 +00:00
|
|
|
- running
|
2011-10-31 01:23:12 +00:00
|
|
|
- watch:
|
|
|
|
- file: /etc/httpd/extra/httpd-vhosts.conf
|
|
|
|
|
|
|
|
/etc/httpd/extra/httpd-vhosts.conf:
|
2012-04-04 05:52:32 +00:00
|
|
|
file.managed:
|
2012-08-03 06:23:39 +00:00
|
|
|
- source: salt://apache/httpd-vhosts.conf
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2013-01-24 06:46:12 +00:00
|
|
|
.. include:: /_incl/extend_with_require_watch.rst
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
:term:`Name declaration`
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
You can override the :term:`ID declaration` by using a :term:`name
|
2012-03-09 04:05:52 +00:00
|
|
|
declaration`. For example, the previous example is a bit more maintainable if
|
|
|
|
rewritten as follows:
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2013-08-12 03:17:47 +00:00
|
|
|
``apache/mywebsite.sls:``
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
.. code-block:: yaml
|
2012-08-03 06:23:39 +00:00
|
|
|
:emphasize-lines: 8,10,12
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
include:
|
2013-02-18 08:47:00 +00:00
|
|
|
- apache.apache
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
extend:
|
2012-07-11 18:59:02 +00:00
|
|
|
apache:
|
2011-10-31 01:23:12 +00:00
|
|
|
service:
|
2013-02-18 08:47:00 +00:00
|
|
|
- running
|
2011-10-31 01:23:12 +00:00
|
|
|
- watch:
|
|
|
|
- file: mywebsite
|
|
|
|
|
|
|
|
mywebsite:
|
2012-04-04 05:52:32 +00:00
|
|
|
file.managed:
|
2011-10-31 01:23:12 +00:00
|
|
|
- name: /etc/httpd/extra/httpd-vhosts.conf
|
2012-08-03 06:23:39 +00:00
|
|
|
- source: salt://apache/httpd-vhosts.conf
|
2011-10-31 01:23:12 +00:00
|
|
|
|
|
|
|
:term:`Names declaration`
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
Even more powerful is using a :term:`names declaration` to override the
|
|
|
|
:term:`ID declaration` for multiple states at once. This often can remove the
|
|
|
|
need for looping in a template. For example, the first example in this tutorial
|
|
|
|
can be rewritten without the loop:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
stooges:
|
2012-04-04 05:52:32 +00:00
|
|
|
user.present:
|
2011-10-31 01:23:12 +00:00
|
|
|
- names:
|
|
|
|
- moe
|
|
|
|
- larry
|
|
|
|
- curly
|
|
|
|
|
2013-07-26 01:41:21 +00:00
|
|
|
Next steps
|
|
|
|
==========
|
2011-10-31 01:23:12 +00:00
|
|
|
|
2013-07-26 01:41:21 +00:00
|
|
|
In :doc:`part 4 <states_pt4>` we will discuss how to use salt's
|
|
|
|
:conf_master:`file_roots` to set up a workflow in which states can be
|
|
|
|
"promoted" from dev, to QA, to production.
|