salt/doc/topics/tutorials/pillar.rst

382 lines
10 KiB
ReStructuredText
Raw Normal View History

2013-03-13 17:20:56 +00:00
==================
Pillar Walkthrough
==================
.. note::
This walkthrough assumes that the reader has already completed the initial
Salt :doc:`walkthrough </topics/tutorials/walkthrough>`.
2013-03-13 17:20:56 +00:00
Pillars are tree-like structures of data defined on the Salt Master and passed
2014-02-27 22:43:24 +00:00
through to minions. They allow confidential, targeted data to be securely sent
only to the relevant minion.
.. note::
Grains and Pillar are sometimes confused, just remember that Grains
2014-05-25 04:42:00 +00:00
are data about a minion which is stored or generated from the minion.
2014-02-27 22:43:24 +00:00
This is why information like the OS and CPU type are found in Grains.
Pillar is information about a minion or many minions stored or generated
on the Salt Master.
Pillar data is useful for:
2013-03-13 17:20:56 +00:00
Highly Sensitive Data:
Information transferred via pillar is guaranteed to only be presented to
the minions that are targeted, making Pillar suitable
for managing security information, such as cryptographic keys and
passwords.
2013-03-13 17:20:56 +00:00
Minion Configuration:
Minion modules such as the execution modules, states, and returners can
often be configured via data stored in pillar.
Variables:
Variables which need to be assigned to specific minions or groups of
minions can be defined in pillar and then accessed inside sls formulas
and template files.
Arbitrary Data:
Pillar can contain any basic data structure in dictionary format,
so a key/value store can be defined making it easy to iterate over a group
of values in sls formulas.
2013-03-13 17:20:56 +00:00
2014-05-25 04:42:00 +00:00
Pillar is therefore one of the most important systems when using Salt. This
2014-02-27 22:43:24 +00:00
walkthrough is designed to get a simple Pillar up and running in a few minutes
and then to dive into the capabilities of Pillar and where the data is
available.
2013-03-13 17:20:56 +00:00
Setting Up Pillar
=================
The pillar is already running in Salt by default. To see the minion's
pillar data:
2013-03-13 17:20:56 +00:00
.. code-block:: bash
salt '*' pillar.items
.. note::
Prior to version 0.16.2, this function is named ``pillar.data``. This
function name is still supported for backwards compatibility.
2013-03-13 17:20:56 +00:00
By default the contents of the master configuration file are loaded into
pillar for all minions. This enables the master configuration file to
2013-03-13 17:20:56 +00:00
be used for global configuration of minions.
Similar to the state tree, the pillar is comprised of sls files and has a top file.
The default location for the pillar is in /srv/pillar.
2013-03-13 17:20:56 +00:00
.. note::
The pillar location can be configured via the `pillar_roots` option inside
the master configuration file. It must not be in a subdirectory of the state
tree or file_roots. If the pillar is under file_roots, any pillar targeting
can be bypassed by minions.
2013-03-13 17:20:56 +00:00
To start setting up the pillar, the /srv/pillar directory needs to be present:
.. code-block:: bash
mkdir /srv/pillar
2013-03-13 17:20:56 +00:00
2014-04-17 12:18:20 +00:00
Now create a simple top file, following the same format as the top file used for
states:
2013-03-13 17:20:56 +00:00
``/srv/pillar/top.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: yaml
base:
'*':
- data
This top file associates the data.sls file to all minions. Now the
``/srv/pillar/data.sls`` file needs to be populated:
2013-03-13 17:20:56 +00:00
``/srv/pillar/data.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: yaml
info: some data
2014-08-25 16:56:55 +00:00
To ensure that the minions have the new pillar data, issue a command
to them asking that they fetch their pillars from the master:
.. code-block:: bash
salt '*' saltutil.refresh_pillar
2014-08-25 16:56:55 +00:00
2014-10-27 23:13:46 +00:00
Now that the minions have the new pillar, it can be retrieved:
2013-03-13 17:20:56 +00:00
.. code-block:: bash
salt '*' pillar.items
2013-03-13 17:20:56 +00:00
The key ``info`` should now appear in the returned pillar data.
2013-03-13 17:20:56 +00:00
More Complex Data
~~~~~~~~~~~~~~~~~
Unlike states, pillar files do not need to define :strong:`formulas`.
This example sets up user data with a UID:
2013-03-13 17:20:56 +00:00
``/srv/pillar/users/init.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: yaml
users:
thatch: 1000
shouse: 1001
utahdave: 1002
redbeard: 1003
.. note::
The same directory lookups that exist in states exist in pillar, so the
file ``users/init.sls`` can be referenced with ``users`` in the :term:`top
file`.
2013-03-13 17:20:56 +00:00
The top file will need to be updated to include this sls file:
2013-03-13 17:20:56 +00:00
``/srv/pillar/top.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: yaml
base:
'*':
- data
- users
Now the data will be available to the minions. To use the pillar data in a
state, you can use Jinja:
2013-03-13 17:20:56 +00:00
``/srv/salt/users/init.sls``
2013-03-13 17:20:56 +00:00
.. code-block:: jinja
2013-03-13 17:20:56 +00:00
{% for user, uid in pillar.get('users', {}).items() %}
{{user}}:
user.present:
- uid: {{uid}}
{% endfor %}
This approach allows for users to be safely defined in a pillar and then the
user data is applied in an sls file.
2014-04-30 20:55:30 +00:00
Parameterizing States With Pillar
2013-03-13 17:20:56 +00:00
=================================
Pillar data can be accessed in state files to customise behavior for each
minion. All pillar (and grain) data applicable to each minion is substituted
into the state files through templating before being run. Typical uses
include setting directories appropriate for the minion and skipping states
that don't apply.
2013-03-13 17:20:56 +00:00
A simple example is to set up a mapping of package names in pillar for
separate Linux distributions:
``/srv/pillar/pkg/init.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: jinja
2013-03-13 17:20:56 +00:00
pkgs:
{% if grains['os_family'] == 'RedHat' %}
2013-03-13 17:20:56 +00:00
apache: httpd
vim: vim-enhanced
{% elif grains['os_family'] == 'Debian' %}
2013-03-13 17:20:56 +00:00
apache: apache2
vim: vim
{% elif grains['os'] == 'Arch' %}
apache: apache
vim: vim
{% endif %}
The new ``pkg`` sls needs to be added to the top file:
2013-03-13 17:20:56 +00:00
``/srv/pillar/top.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: yaml
base:
'*':
- data
- users
- pkg
Now the minions will auto map values based on respective operating systems
inside of the pillar, so sls files can be safely parameterized:
``/srv/salt/apache/init.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: jinja
2013-03-13 17:20:56 +00:00
apache:
pkg.installed:
- name: {{ pillar['pkgs']['apache'] }}
Or, if no pillar is available a default can be set as well:
.. note::
The function ``pillar.get`` used in this example was added to Salt in
version 0.14.0
``/srv/salt/apache/init.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: jinja
2013-03-13 17:20:56 +00:00
apache:
pkg.installed:
- name: {{ salt['pillar.get']('pkgs:apache', 'httpd') }}
2013-03-13 17:20:56 +00:00
In the above example, if the pillar value ``pillar['pkgs']['apache']`` is not
set in the minion's pillar, then the default of ``httpd`` will be used.
2013-03-13 17:20:56 +00:00
.. note::
2013-03-13 17:20:56 +00:00
Under the hood, pillar is just a Python dict, so Python dict methods such
2013-03-13 17:20:56 +00:00
as `get` and `items` can be used.
Pillar Makes Simple States Grow Easily
======================================
One of the design goals of pillar is to make simple sls formulas easily grow
into more flexible formulas without refactoring or complicating the states.
A simple formula:
``/srv/salt/edit/vim.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: yaml
vim:
pkg.installed: []
2013-03-13 17:20:56 +00:00
/etc/vimrc:
file.managed:
- source: salt://edit/vimrc
- mode: 644
- user: root
- group: root
- require:
- pkg: vim
Can be easily transformed into a powerful, parameterized formula:
``/srv/salt/edit/vim.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: jinja
2013-03-13 17:20:56 +00:00
vim:
pkg.installed:
2013-03-13 17:20:56 +00:00
- name: {{ pillar['pkgs']['vim'] }}
/etc/vimrc:
file.managed:
- source: {{ pillar['vimrc'] }}
- mode: 644
- user: root
- group: root
- require:
- pkg: vim
Where the vimrc source location can now be changed via pillar:
``/srv/pillar/edit/vim.sls``:
2013-03-13 17:20:56 +00:00
.. code-block:: jinja
2013-03-13 17:20:56 +00:00
{% if grains['id'].startswith('dev') %}
2013-03-13 17:20:56 +00:00
vimrc: salt://edit/dev_vimrc
{% elif grains['id'].startswith('qa') %}
2013-03-13 17:20:56 +00:00
vimrc: salt://edit/qa_vimrc
{% else %}
vimrc: salt://edit/vimrc
{% endif %}
Ensuring that the right vimrc is sent out to the correct minions.
Setting Pillar Data on the Command Line
=======================================
Pillar data can be set on the command line when running :py:func:`state.apply
<salt.modules.state.apply_` like so:
.. code-block:: bash
salt '*' state.apply pillar='{"foo": "bar"}'
salt '*' state.apply my_sls_file pillar='{"hello": "world"}'
2015-10-23 21:58:19 +00:00
Nested pillar values can also be set via the command line:
.. code-block:: bash
salt '*' state.sls my_sls_file pillar='{"foo": {"bar": "baz"}}'
.. note::
If a key is passed on the command line that already exists on the minion,
the key that is passed in will overwrite the entire value of that key,
rather than merging only the specified value set via the command line.
The example below will swap the value for vim with telnet in the previously
specified list, notice the nested pillar dict:
.. code-block:: bash
salt '*' state.apply edit.vim pillar='{"pkgs": {"vim": "telnet"}}'
This will attempt to install telnet on your minions, feel free to
uninstall the package or replace telnet value with anything else.
.. note::
Be aware that when sending sensitive data via pillar on the command-line
that the publication containing that data will be received by all minions
and will not be restricted to the targeted minions. This may represent
a security concern in some cases.
2013-03-13 17:20:56 +00:00
More On Pillar
==============
Pillar data is generated on the Salt master and securely distributed to
2013-03-13 17:20:56 +00:00
minions. Salt is not restricted to the pillar sls files when defining the
pillar but can retrieve data from external sources. This can be useful when
information about an infrastructure is stored in a separate location.
Reference information on pillar and the external pillar interface can be found
in the Salt documentation:
2013-03-13 17:20:56 +00:00
:doc:`Pillar </topics/pillar/index>`
Minion Config in Pillar
=======================
Minion configuration options can be set on pillars. Any option that you want
to modify, should be in the first level of the pillars, in the same way you set
the options in the config file. For example, to configure the MySQL root
password to be used by MySQL Salt execution module:
.. code-block:: yaml
mysql.pass: hardtoguesspassword
This is very convenient when you need some dynamic configuration change that
you want to be applied on the fly. For example, there is a chicken and the egg
problem if you do this:
.. code-block:: yaml
mysql-admin-passwd:
mysql_user.present:
- name: root
- password: somepasswd
mydb:
mysql_db.present
The second state will fail, because you changed the root password and the
minion didn't notice it. Setting mysql.pass in the pillar, will help to sort
out the issue. But always change the root admin password in the first place.
This is very helpful for any module that needs credentials to apply state
changes: mysql, keystone, etc.