salt/doc/ref/renderers/index.rst

90 lines
2.9 KiB
ReStructuredText
Raw Normal View History

=========
Renderers
=========
2011-07-02 23:40:40 +00:00
The Salt state system operates by gathering information from simple data
structures. The state system was designed in this way to make interacting with
2012-05-23 04:43:12 +00:00
it generic and simple. This also means that state files (SLS files) can be one
2011-07-02 23:40:40 +00:00
of many formats.
2012-05-23 04:43:12 +00:00
By default SLS files are rendered as Jinja templates and then parsed as YAML
2011-07-02 23:40:40 +00:00
documents. But since the only thing the state system cares about is raw data,
2012-05-23 04:43:12 +00:00
the SLS files can be any structured format that can be dreamed up.
2011-07-02 23:40:40 +00:00
Currently there is support for ``Jinja + YAML``, ``Mako + YAML``,
``Wempy + YAML``, ``Jinja + json`` ``Mako + json`` and ``Wempy + json``. But
renderers can be written to support anything. This means that the Salt states
could be managed by xml files, html files, puppet files, or any format that
can be translated into the data structure used by the state system.
2011-07-02 23:40:40 +00:00
2012-02-09 18:13:55 +00:00
Multiple Renderers
------------------
When deploying a state tree a default renderer is selected in the master
configuration file with the renderer option. But multiple renderers can be
used inside the same state tree.
2012-05-23 04:43:12 +00:00
When rendering SLS files Salt checks for the presence of a Salt specific
2012-02-09 18:13:55 +00:00
shebang line. The shebang line syntax was chosen because it is familiar to
the target audience, the systems admin and systems engineer.
The shebang line directly calls the name of the renderer as it is specified
within Salt. One of the most common reasons to use multiple renderers in to
2012-05-23 04:43:12 +00:00
use the Python or ``py`` renderer:
2012-02-09 18:13:55 +00:00
.. code-block:: python
#!py
def run():
'''
Install the python-mako package
'''
return {'include': ['python'],
'python-mako': {'pkg': ['installed']}}
The first line is a shebang that references the ``py`` renderer.
2011-07-02 23:40:40 +00:00
Writing Renderers
-----------------
2012-05-23 04:43:12 +00:00
Writing a renderer is easy, all that is required is that a Python module
2011-07-02 23:40:40 +00:00
is placed in the rendered directory and that the module implements the
2012-05-23 04:43:12 +00:00
render function. The render function will be passed the path of the SLS file.
2011-07-02 23:40:40 +00:00
In the render function, parse the passed file and return the data structure
derived from the file. You can place your custom renderers in a ``_renderers``
directory in your file root (``/srv/salt/``).
2011-07-02 23:40:40 +00:00
Examples
--------
The best place to find examples of renderers is in the Salt source code. The
renderers included with Salt can be found here:
:blob:`salt/renderers`
2011-07-02 23:40:40 +00:00
2012-05-23 04:43:12 +00:00
Here is a simple Jinja + YAML example:
2011-07-02 23:40:40 +00:00
.. code-block:: python
2012-05-23 04:43:12 +00:00
# Import Python libs
2011-07-02 23:40:40 +00:00
import os
# Import Third Party libs
import yaml
from jinja2 import Template
def render(template):
'''
Render the data passing the functions and grains into the rendering system
'''
if not os.path.isfile(template):
return {}
passthrough = {}
passthrough.update(__salt__)
passthrough.update(__grains__)
template = Template(open(template, 'r').read())
yaml_data = template.render(**passthrough)
return yaml.load(yaml_data)