2015-06-05 23:32:35 +00:00
|
|
|
.. _external-pillars:
|
|
|
|
|
|
|
|
================
|
2013-03-29 18:18:43 +00:00
|
|
|
External Pillars
|
2015-06-05 23:32:35 +00:00
|
|
|
================
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
Salt provides a mechanism for generating pillar data by calling external
|
|
|
|
pillar interfaces. This document will describe an outline of an ext_pillar
|
|
|
|
module.
|
|
|
|
|
|
|
|
Location
|
|
|
|
--------
|
|
|
|
|
2013-06-30 09:01:20 +00:00
|
|
|
Salt expects to find your ``ext_pillar`` module in the same location where it
|
2013-03-29 18:18:43 +00:00
|
|
|
looks for other python modules. If the ``extension_modules`` option in your
|
2013-06-30 09:01:20 +00:00
|
|
|
Salt master configuration is set, Salt will look for a ``pillar`` directory
|
2013-03-29 18:18:43 +00:00
|
|
|
under there and load all the modules it finds. Otherwise, it will look in
|
2013-06-30 09:01:20 +00:00
|
|
|
your Python site-packages ``salt/pillar`` directory.
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
Configuration
|
|
|
|
-------------
|
|
|
|
|
2013-06-20 21:29:59 +00:00
|
|
|
The external pillars that are called when a minion refreshes its pillars is
|
2013-03-29 18:18:43 +00:00
|
|
|
controlled by the ``ext_pillar`` option in the Salt master configuration. You
|
2013-06-20 21:29:59 +00:00
|
|
|
can pass a single argument, a list of arguments or a dictionary of arguments
|
2013-03-29 18:18:43 +00:00
|
|
|
to your pillar:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
ext_pillar:
|
|
|
|
- example_a: some argument
|
|
|
|
- example_b:
|
|
|
|
- argumentA
|
|
|
|
- argumentB
|
|
|
|
- example_c:
|
|
|
|
keyA: valueA
|
|
|
|
keyB: valueB
|
|
|
|
|
|
|
|
|
|
|
|
The Module
|
|
|
|
----------
|
|
|
|
|
|
|
|
Imports and Logging
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
Import modules your external pillar module needs. You should first include
|
|
|
|
generic modules that come with stock Python:
|
|
|
|
|
2013-03-29 19:03:37 +00:00
|
|
|
.. code-block:: python
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
|
|
And then start logging. This is an idiomatic way of setting up logging in Salt:
|
|
|
|
|
2013-03-29 19:03:37 +00:00
|
|
|
.. code-block:: python
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
Finally, load modules that are specific to what you are doing. You should catch
|
2014-03-06 22:38:27 +00:00
|
|
|
import errors and set a flag that the ``__virtual__`` function can use later.
|
2013-03-29 18:18:43 +00:00
|
|
|
|
2013-03-29 19:03:37 +00:00
|
|
|
.. code-block:: python
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import weird_thing
|
2014-03-06 22:38:27 +00:00
|
|
|
EXAMPLE_A_LOADED = True
|
2013-03-29 18:18:43 +00:00
|
|
|
except ImportError:
|
2014-03-06 22:38:27 +00:00
|
|
|
EXAMPLE_A_LOADED = False
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
Options
|
|
|
|
-------
|
|
|
|
|
2013-06-20 21:29:59 +00:00
|
|
|
If you define an ``__opts__`` dictionary, it will be merged into the
|
2013-03-29 18:18:43 +00:00
|
|
|
``__opts__`` dictionary handed to the ``ext_pillar`` function later. This is a
|
2013-06-20 21:29:59 +00:00
|
|
|
good place to put default configuration items. The convention is to name
|
2013-03-29 18:18:43 +00:00
|
|
|
things ``modulename.option``.
|
|
|
|
|
2013-03-29 19:03:37 +00:00
|
|
|
.. code-block:: python
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
__opts__ = { 'example_a.someconfig': 137 }
|
|
|
|
|
|
|
|
|
|
|
|
Initialization
|
|
|
|
--------------
|
|
|
|
|
|
|
|
If you define an ``__init__`` function, it will be called with the following
|
|
|
|
signature:
|
|
|
|
|
2013-03-29 19:03:37 +00:00
|
|
|
.. code-block:: python
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
def __init__( __opts__ ):
|
|
|
|
# Do init work here
|
|
|
|
|
|
|
|
|
|
|
|
**Note**: The ``__init__`` function is ran every time a particular minion causes
|
|
|
|
the external pillar to be called, so don't put heavy initialization code here.
|
|
|
|
The ``__init__`` functionality is a side-effect of the Salt loader, so it may
|
|
|
|
not be as useful in pillars as it is in other Salt items.
|
|
|
|
|
|
|
|
__virtual__
|
|
|
|
-----------
|
|
|
|
|
|
|
|
If you define a ``__virtual__`` function, you can control whether or not this
|
|
|
|
module is visible. If it returns ``False`` then Salt ignores this module. If
|
|
|
|
it returns a string, then that string will be how Salt identifies this external
|
2014-12-11 15:51:43 +00:00
|
|
|
pillar in its ``ext_pillar`` configuration. If you're not renaming the module,
|
|
|
|
simply return ``True`` in the ``__virtual__`` function, which is the same as if
|
|
|
|
this function did not exist, then, the name Salt's ``ext_pillar`` will use to
|
2014-03-06 22:38:27 +00:00
|
|
|
identify this module is its conventional name in Python.
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
This is useful to write modules that can be installed on all Salt masters, but
|
|
|
|
will only be visible if a particular piece of software your module requires is
|
|
|
|
installed.
|
|
|
|
|
2013-03-29 19:03:37 +00:00
|
|
|
.. code-block:: python
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
# This external pillar will be known as `example_a`
|
|
|
|
def __virtual__():
|
2014-03-06 22:38:27 +00:00
|
|
|
if EXAMPLE_A_LOADED:
|
|
|
|
return True
|
|
|
|
return False
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
|
2013-03-29 19:03:37 +00:00
|
|
|
.. code-block:: python
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
# This external pillar will be known as `something_else`
|
2014-03-06 22:38:27 +00:00
|
|
|
__virtualname__ = 'something_else'
|
|
|
|
|
2013-03-29 18:18:43 +00:00
|
|
|
def __virtual__():
|
2014-03-06 22:38:27 +00:00
|
|
|
if EXAMPLE_A_LOADED:
|
|
|
|
return __virtualname__
|
|
|
|
return False
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
ext_pillar
|
|
|
|
----------
|
|
|
|
|
|
|
|
This is where the real work of an external pillar is done. If this module is
|
|
|
|
active and has a function called ``ext_pillar``, whenever a minion updates its
|
|
|
|
pillar this function is called.
|
|
|
|
|
|
|
|
How it is called depends on how it is configured in the Salt master
|
|
|
|
configuration. The first argument is always the current pillar dictionary, this
|
|
|
|
contains pillar items that have already been added, starting with the data from
|
2013-06-20 21:29:59 +00:00
|
|
|
``pillar_roots``, and then from any already-ran external pillars.
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
Using our example above:
|
|
|
|
|
2013-03-29 19:03:37 +00:00
|
|
|
.. code-block:: python
|
2013-03-29 18:18:43 +00:00
|
|
|
|
2013-11-12 22:38:40 +00:00
|
|
|
ext_pillar( id, pillar, 'some argument' ) # example_a
|
|
|
|
ext_pillar( id, pillar, 'argumentA', 'argumentB' ) # example_b
|
|
|
|
ext_pillar( id, pillar, keyA='valueA', keyB='valueB' } ) # example_c
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
|
2013-06-20 21:29:59 +00:00
|
|
|
In the ``example_a`` case, ``pillar`` will contain the items from the
|
2013-03-29 18:18:43 +00:00
|
|
|
``pillar_roots``, in ``example_b`` ``pillar`` will contain that plus the items
|
|
|
|
added by ``example_a``, and in ``example_c`` ``pillar`` will contain that plus
|
2013-11-12 22:38:40 +00:00
|
|
|
the items added by ``example_b``. In all three cases, ``id`` will contain the
|
|
|
|
ID of the minion making the pillar request.
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
This function should return a dictionary, the contents of which are merged in
|
|
|
|
with all of the other pillars and returned to the minion. **Note**: this function
|
|
|
|
is called once for each minion that fetches its pillar data.
|
|
|
|
|
2013-03-29 19:03:37 +00:00
|
|
|
.. code-block:: python
|
2013-03-29 18:18:43 +00:00
|
|
|
|
2013-11-12 22:38:40 +00:00
|
|
|
def ext_pillar( minion_id, pillar, *args, **kwargs ):
|
2013-06-20 21:29:59 +00:00
|
|
|
|
2015-07-13 18:56:28 +00:00
|
|
|
my_pillar = {'external_pillar': {}}
|
2013-03-29 18:18:43 +00:00
|
|
|
|
2015-07-13 18:56:28 +00:00
|
|
|
my_pillar['external_pillar'] = get_external_pillar_dictionary()
|
2013-03-29 18:18:43 +00:00
|
|
|
|
2013-03-29 18:28:49 +00:00
|
|
|
return my_pillar
|
2013-06-20 21:29:59 +00:00
|
|
|
|
2013-03-29 18:18:43 +00:00
|
|
|
|
2015-07-13 18:56:28 +00:00
|
|
|
You can call pillar with the dictionary's top name to retrieve its data.
|
|
|
|
From above example, 'external_pillar' is the top dictionary name. Therefore:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
2015-07-14 21:26:00 +00:00
|
|
|
|
2015-07-13 18:56:28 +00:00
|
|
|
salt-call '*' pillar.get external_pillar
|
|
|
|
|
|
|
|
|
2013-03-29 18:28:49 +00:00
|
|
|
You shouldn't just add items to ``pillar`` and return that, since that will
|
|
|
|
cause Salt to merge data that already exists. Rather, just return the items
|
|
|
|
you are adding or changing. You could, however, use ``pillar`` in your module
|
|
|
|
to make some decision based on pillar data that already exists.
|
|
|
|
|
2013-03-29 18:18:43 +00:00
|
|
|
This function has access to some useful globals:
|
|
|
|
|
2013-06-20 21:29:59 +00:00
|
|
|
:__opts__:
|
2013-03-29 18:18:43 +00:00
|
|
|
A dictionary of mostly Salt configuration options. If you had an
|
2013-06-20 21:29:59 +00:00
|
|
|
``__opts__`` dictionary defined in your module, those values will be
|
2013-11-12 22:38:40 +00:00
|
|
|
included.
|
2013-03-29 18:18:43 +00:00
|
|
|
|
|
|
|
:__salt__:
|
|
|
|
A dictionary of Salt module functions, useful so you don't have to
|
2013-06-20 21:29:59 +00:00
|
|
|
duplicate functions that already exist. E.g.
|
2013-03-29 18:18:43 +00:00
|
|
|
``__salt__['cmd.run']( 'ls -l' )`` **Note**, runs on the *master*
|
|
|
|
|
|
|
|
:__grains__:
|
|
|
|
A dictionary of the grains of the minion making this pillar call.
|
|
|
|
|
2013-06-20 21:32:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
Example configuration
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
As an example, if you wanted to add external pillar via the ``cmd_json``
|
|
|
|
external pillar, add something like this to your master config:
|
|
|
|
|
|
|
|
.. code-block:: yaml
|
|
|
|
|
|
|
|
ext_pillar:
|
2014-02-28 07:19:37 +00:00
|
|
|
- cmd_json: 'echo {\"arg\":\"value\"}'
|
2014-08-25 16:56:55 +00:00
|
|
|
|
|
|
|
Reminder
|
|
|
|
--------
|
|
|
|
|
2014-12-11 15:51:43 +00:00
|
|
|
Just as with traditional pillars, external pillars must be refreshed in order for
|
2014-08-25 16:56:55 +00:00
|
|
|
minions to see any fresh data:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
|
|
|
|
2015-07-13 18:56:28 +00:00
|
|
|
salt '*' saltutil.refresh_pillar
|