2014-01-07 23:55:28 +00:00
|
|
|
=================
|
|
|
|
Execution Modules
|
|
|
|
=================
|
2011-05-23 07:17:30 +00:00
|
|
|
|
2014-01-07 23:55:28 +00:00
|
|
|
Salt execution modules are the functions called by the :command:`salt` command.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
Salt execution modules are different from state modules and cannot be
|
2014-02-19 19:51:53 +00:00
|
|
|
called directly within state files. You must use the :mod:`module <salt.states.module>`
|
|
|
|
state module to call execution modules within state runs.
|
2011-05-23 07:17:30 +00:00
|
|
|
|
2011-11-13 10:06:00 +00:00
|
|
|
.. seealso:: :ref:`Full list of builtin modules <all-salt.modules>`
|
2011-06-25 07:34:33 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Salt ships with many modules that cover a wide variety of tasks.
|
2011-06-25 07:34:33 +00:00
|
|
|
|
2013-02-11 04:20:56 +00:00
|
|
|
Modules Are Easy to Write!
|
|
|
|
==========================
|
2011-05-23 07:17:30 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Writing Salt execution modules is straightforward.
|
|
|
|
|
|
|
|
A Salt execution modules is a Python or `Cython`_ module
|
|
|
|
placed in a directory called ``_modules/``
|
|
|
|
within the :conf_master:`file_roots` as specified by the master config file. By
|
|
|
|
default this is `/srv/salt/_modules` on Linux systems.
|
|
|
|
|
|
|
|
|
|
|
|
Modules placed in ``_modules/`` will be synced to the minions when any of the following
|
|
|
|
Salt functions are called:
|
2014-02-26 20:28:13 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
:mod:`state.highstate <salt.modules.state.highstate>`
|
|
|
|
|
|
|
|
:mod:`saltutil.sync_modules <salt.modules.saltutil.sync_modules>`
|
|
|
|
|
|
|
|
:mod:`saltutil.sync_all <salt.modules.saltutil.sync_all>`
|
|
|
|
|
|
|
|
Note that a module's default name is its filename
|
2013-07-09 02:36:34 +00:00
|
|
|
(i.e. ``foo.py`` becomes module ``foo``), but that its name can be overridden
|
|
|
|
by using a :ref:`__virtual__ function <virtual-modules>`.
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
If a Salt module has errors and cannot be imported, the Salt minion will continue
|
|
|
|
to load without issue and the module with errors will simply be omitted.
|
2011-05-23 07:17:30 +00:00
|
|
|
|
|
|
|
If adding a Cython module the file must be named ``<modulename>.pyx`` so that
|
|
|
|
the loader knows that the module needs to be imported as a Cython module. The
|
|
|
|
compilation of the Cython module is automatic and happens when the minion
|
|
|
|
starts, so only the ``*.pyx`` file is required.
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
.. _`Cython`: http://cython.org/
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Cross-Calling Modules
|
2011-06-22 03:29:51 +00:00
|
|
|
=====================
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
All of the Salt execution modules are available to each other and modules can call
|
|
|
|
functions available in other execution modules.
|
2011-06-22 03:29:51 +00:00
|
|
|
|
|
|
|
The variable ``__salt__`` is packed into the modules after they are loaded into
|
2014-02-19 19:51:53 +00:00
|
|
|
the Salt minion.
|
2011-06-22 03:29:51 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
The ``__salt__`` variable is a :ref:`Python dictionary <python2:typesmapping>`
|
|
|
|
containing all of the Salt functions. Dictionary keys are strings representing the
|
|
|
|
names of the modules and the values are the functions themselves.
|
|
|
|
|
|
|
|
Salt modules can be cross-called by accessing the value in the ``__salt__`` dict:
|
2011-06-22 03:29:51 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
2012-03-15 00:09:19 +00:00
|
|
|
|
2011-06-22 03:29:51 +00:00
|
|
|
def foo(bar):
|
|
|
|
return __salt__['cmd.run'](bar)
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
This code will call the `run` function in the :mod:`cmd <salt.modules.cmdmod>` and pass the argument
|
|
|
|
``bar`` to it.
|
2011-06-22 03:29:51 +00:00
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Preloaded Execution Module Data
|
|
|
|
===============================
|
|
|
|
|
|
|
|
When interacting with execution modules often it is nice to be able to read information
|
|
|
|
dynamically about the minion or to load in configuration parameters for a module.
|
2011-05-23 07:17:30 +00:00
|
|
|
|
|
|
|
Salt allows for different types of data to be loaded into the modules by the
|
2014-02-19 19:51:53 +00:00
|
|
|
minion.
|
2011-05-23 07:17:30 +00:00
|
|
|
|
|
|
|
Grains Data
|
|
|
|
-----------
|
|
|
|
|
2013-07-23 16:20:50 +00:00
|
|
|
The values detected by the Salt Grains on the minion are available in a
|
|
|
|
:ref:`dict <python2:typesmapping>` named ``__grains__`` and can be accessed
|
|
|
|
from within callable objects in the Python modules.
|
2011-05-23 07:17:30 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
To see the contents of the grains dictionary for a given system in your deployment
|
2011-05-23 07:17:30 +00:00
|
|
|
run the :func:`grains.items` function:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
salt 'hostname' grains.items --output=pprint
|
2011-05-23 07:17:30 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Any value in a grains dictionary can be accessed as any other Python dictionary. For
|
|
|
|
example, the grain representing the minion ID is stored in the ``id`` key and from
|
|
|
|
an execution module, the value would be stored in ``__grains__['id']``.
|
2011-05-23 07:17:30 +00:00
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
|
2011-05-23 07:17:30 +00:00
|
|
|
Module Configuration
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
Since parameters for configuring a module may be desired, Salt allows for
|
2014-02-19 19:51:53 +00:00
|
|
|
configuration information from the minion configuation file to be passed to
|
|
|
|
execution modules.
|
2011-05-23 07:17:30 +00:00
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
Since the minion configuration file is a YAML document, arbitrary configuration
|
2014-02-19 19:51:53 +00:00
|
|
|
data can be passed in the minion config that is read by the modules. It is therefore
|
2011-05-23 07:17:30 +00:00
|
|
|
**strongly** recommended that the values passed in the configuration file match
|
2014-02-19 19:51:53 +00:00
|
|
|
the module name. A value intended for the ``test`` execution module should be named
|
|
|
|
``test.<value>``.
|
2011-05-23 07:17:30 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
The test execution module contains usage of the module configuration and the default
|
2011-05-23 07:17:30 +00:00
|
|
|
configuration file for the minion contains the information and format used to
|
|
|
|
pass data to the modules. :mod:`salt.modules.test`, :file:`conf/minion`.
|
|
|
|
|
2011-07-09 20:24:54 +00:00
|
|
|
Printout Configuration
|
|
|
|
======================
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Since execution module functions can return different data, and the way the data is
|
|
|
|
printed can greatly change the presentation, Salt has a printout configuration.
|
2011-07-09 20:24:54 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
When writing a module the ``__outputter__`` dictionary can be declared in the module.
|
|
|
|
The ``__outputter__`` dictionary contains a mapping of function name to Salt
|
2011-07-09 20:24:54 +00:00
|
|
|
Outputter.
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2011-07-17 23:13:32 +00:00
|
|
|
__outputter__ = {
|
2011-07-09 20:24:54 +00:00
|
|
|
'run': 'txt'
|
|
|
|
}
|
|
|
|
|
|
|
|
This will ensure that the text outputter is used.
|
|
|
|
|
2013-07-09 02:36:34 +00:00
|
|
|
|
|
|
|
.. _virtual-modules:
|
|
|
|
|
2011-06-22 04:44:10 +00:00
|
|
|
Virtual Modules
|
|
|
|
===============
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Sometimes an execution module should be presented in a generic way. A good example of this
|
2011-06-22 04:44:10 +00:00
|
|
|
can be found in the package manager modules. The package manager changes from
|
2014-02-19 19:51:53 +00:00
|
|
|
one operating system to another, but the Salt execution module that interfaces with the
|
2011-06-22 04:44:10 +00:00
|
|
|
package manager can be presented in a generic way.
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
The Salt modules for package managers all contain a ``__virtual__`` function
|
2011-06-22 04:44:10 +00:00
|
|
|
which is called to define what systems the module should be loaded on.
|
|
|
|
|
2013-07-23 16:55:45 +00:00
|
|
|
The ``__virtual__`` function is used to return either a
|
2013-07-23 17:21:01 +00:00
|
|
|
:ref:`string <python2:typesseq>` or :py:data:`False`. If
|
2011-06-22 04:44:10 +00:00
|
|
|
False is returned then the module is not loaded, if a string is returned then
|
|
|
|
the module is loaded with the name of the string.
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
This means that the package manager modules can be presented as the ``pkg`` module
|
2011-06-22 04:44:10 +00:00
|
|
|
regardless of what the actual module is named.
|
|
|
|
|
|
|
|
The package manager modules are the best example of using the ``__virtual__``
|
2013-11-09 21:10:12 +00:00
|
|
|
function. Some examples:
|
|
|
|
|
|
|
|
- :blob:`pacman.py <salt/modules/pacman.py>`
|
|
|
|
- :blob:`yumpkg.py <salt/modules/yumpkg.py>`
|
2014-02-20 23:16:59 +00:00
|
|
|
- :blob:`aptpkg.py <salt/modules/aptpkg.py>`
|
2011-06-22 04:44:10 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
.. note::
|
|
|
|
Modules which return a string from ``__virtual__`` that is already used by a module that
|
|
|
|
ships with Salt will _override_ the stock module.
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
|
2011-05-23 07:17:30 +00:00
|
|
|
Documentation
|
|
|
|
=============
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Salt execution modules are documented. The :func:`sys.doc` function will return the
|
2012-08-30 21:28:52 +00:00
|
|
|
documentation for all available modules:
|
2011-05-23 07:17:30 +00:00
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
|
|
|
salt '*' sys.doc
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
The ``sys.doc`` function simply prints out the docstrings found in the modules; when
|
|
|
|
writing Salt execution modules, please follow the formatting conventions for docstrings as
|
2011-05-23 07:17:30 +00:00
|
|
|
they appear in the other modules.
|
|
|
|
|
2011-06-22 04:44:10 +00:00
|
|
|
Adding Documentation to Salt Modules
|
|
|
|
------------------------------------
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
It is strongly suggested that all Salt modules have documentation added.
|
2011-06-22 04:44:10 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
To add documenation add a `Python docstring`_ to the function.
|
2011-06-22 04:44:10 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def spam(eggs):
|
|
|
|
'''
|
|
|
|
A function to make some spam with eggs!
|
|
|
|
|
2012-05-13 18:50:33 +00:00
|
|
|
CLI Example::
|
|
|
|
|
|
|
|
salt '*' test.spam eggs
|
2011-06-22 04:44:10 +00:00
|
|
|
'''
|
|
|
|
return eggs
|
|
|
|
|
|
|
|
Now when the sys.doc call is executed the docstring will be cleanly returned
|
|
|
|
to the calling terminal.
|
|
|
|
|
2013-07-23 20:45:26 +00:00
|
|
|
.. _`Python docstring`: http://docs.python.org/2/glossary.html#term-docstring
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Documentation added to execution modules in docstrings will automatically be added
|
|
|
|
to the online web-based documentation.
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Add Execution Module Metadata
|
|
|
|
-----------------------------
|
2012-12-03 18:39:38 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
When writing a Python docstring for an execution module, add information about the module
|
|
|
|
using the following field lists:
|
2012-12-03 18:39:38 +00:00
|
|
|
|
|
|
|
.. code-block:: text
|
|
|
|
|
|
|
|
:maintainer: Thomas Hatch <thatch@saltstack.com, Seth House <shouse@saltstack.com>
|
|
|
|
:maturity: new
|
|
|
|
:depends: python-mysqldb
|
|
|
|
:platform: all
|
|
|
|
|
2013-02-18 08:11:51 +00:00
|
|
|
The maintainer field is a comma-delimited list of developers who help maintain
|
2012-12-03 18:39:38 +00:00
|
|
|
this module.
|
|
|
|
|
|
|
|
The maturity field indicates the level of quality and testing for this module.
|
|
|
|
Standard labels will be determined.
|
|
|
|
|
|
|
|
The depends field is a comma-delimited list of modules that this module depends
|
|
|
|
on.
|
|
|
|
|
2013-02-18 07:26:28 +00:00
|
|
|
The platform field is a comma-delimited list of platforms that this module is
|
2012-12-03 18:39:38 +00:00
|
|
|
known to run on.
|
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
Private Functions
|
|
|
|
=================
|
2011-05-23 07:17:30 +00:00
|
|
|
|
2014-02-19 19:51:53 +00:00
|
|
|
In Salt, Python callable objects contained within an execution module are made available
|
2012-12-03 18:39:38 +00:00
|
|
|
to the Salt minion for use. The only exception to this rule is a callable
|
|
|
|
object with a name starting with an underscore ``_``.
|
2011-05-23 07:17:30 +00:00
|
|
|
|
|
|
|
Objects Loaded Into the Salt Minion
|
|
|
|
-----------------------------------
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def foo(bar):
|
|
|
|
return bar
|
|
|
|
|
|
|
|
class baz:
|
|
|
|
def __init__(self, quo):
|
2012-05-13 20:49:43 +00:00
|
|
|
pass
|
2011-05-23 07:17:30 +00:00
|
|
|
|
|
|
|
Objects NOT Loaded into the Salt Minion
|
|
|
|
---------------------------------------
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
def _foobar(baz): # Preceded with an _
|
|
|
|
return baz
|
|
|
|
|
2012-05-23 04:43:12 +00:00
|
|
|
cheese = {} # Not a callable Python object
|
2011-06-22 04:44:10 +00:00
|
|
|
|
2013-10-16 17:10:02 +00:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Some callable names also end with an underscore ``_``, to avoid keyword clashes
|
2014-02-19 19:51:53 +00:00
|
|
|
with Python keywords. When using execution modules, or state modules, with these
|
2013-10-16 17:10:02 +00:00
|
|
|
in them the trailing underscore should be omitted.
|
|
|
|
|
2013-06-15 00:19:53 +00:00
|
|
|
Useful Decorators for Modules
|
2013-06-26 17:29:40 +00:00
|
|
|
=============================
|
2013-06-15 00:19:53 +00:00
|
|
|
|
|
|
|
Depends Decorator
|
|
|
|
-----------------
|
2014-02-19 19:51:53 +00:00
|
|
|
When writing execution modules there are many times where some of the module will
|
|
|
|
work on all hosts but some functions have an external dependency, such as a service
|
|
|
|
that needs to be installed or a binary that needs to be present on the system.
|
|
|
|
|
|
|
|
Instead of trying to wrap much of the code in large try/except blocks, a decorator can
|
|
|
|
be used.
|
|
|
|
|
|
|
|
If the dependencies passed to the decorator don't exist, then the salt minion will remove
|
|
|
|
those functions from the module on that host.
|
|
|
|
|
2013-11-15 20:50:17 +00:00
|
|
|
If a "fallback_function" is defined, it will replace the function instead of removing it
|
2013-06-15 00:19:53 +00:00
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
2014-02-19 16:38:38 +00:00
|
|
|
import logging
|
|
|
|
|
2013-06-15 00:19:53 +00:00
|
|
|
from salt.utils.decorators import depends
|
2014-02-19 16:38:38 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2013-06-15 00:19:53 +00:00
|
|
|
try:
|
2013-07-05 20:35:51 +00:00
|
|
|
import dependency_that_sometimes_exists
|
2014-01-15 14:04:03 +00:00
|
|
|
except ImportError as e:
|
2014-01-16 17:49:41 +00:00
|
|
|
log.trace('Failed to import dependency_that_sometimes_exists: {0}'.format(e))
|
2013-06-15 00:19:53 +00:00
|
|
|
|
2013-07-05 20:35:51 +00:00
|
|
|
@depends('dependency_that_sometimes_exists')
|
2013-06-15 00:19:53 +00:00
|
|
|
def foo():
|
|
|
|
'''
|
2013-07-05 20:35:51 +00:00
|
|
|
Function with a dependency on the "dependency_that_sometimes_exists" module,
|
|
|
|
if the "dependency_that_sometimes_exists" is missing this function will not exist
|
2013-06-15 00:19:53 +00:00
|
|
|
'''
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _fallback():
|
|
|
|
'''
|
|
|
|
Fallback function for the depends decorator to replace a function with
|
|
|
|
'''
|
2013-07-05 20:35:51 +00:00
|
|
|
return '"dependency_that_sometimes_exists" needs to be installed for this function to exist'
|
2013-06-15 00:19:53 +00:00
|
|
|
|
2013-11-15 20:50:17 +00:00
|
|
|
@depends('dependency_that_sometimes_exists', fallback_function=_fallback)
|
2013-06-15 00:19:53 +00:00
|
|
|
def foo():
|
|
|
|
'''
|
2013-07-05 20:35:51 +00:00
|
|
|
Function with a dependency on the "dependency_that_sometimes_exists" module.
|
|
|
|
If the "dependency_that_sometimes_exists" is missing this function will be
|
2013-06-15 00:19:53 +00:00
|
|
|
replaced with "_fallback"
|
|
|
|
'''
|
|
|
|
return True
|
|
|
|
|
|
|
|
|