Added salt.pillar.extra_minion_data_in_pillar that adds any extra minion data into the pillar

This commit is contained in:
Alexandru Bleotu 2017-09-28 06:29:55 -04:00
parent 6a25bf4475
commit 1a12d5cb30

View file

@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-
'''
Add all extra minion data to the pillar.
:codeauthor: Alexandru.Bleotu@morganstanley.ms.com
One can filter on the keys to include in the pillar by using the ``include``
parameter. For subkeys the ':' notation is supported (i.e. 'key:subkey')
The keyword ``<all>`` includes all keys.
Complete example in etc/salt/master
=====================================
.. code-block:: yaml
ext_pillar:
- extra_minion_data_in_pillar:
include: <all>
ext_pillar:
- extra_minion_data_in_pillar:
include:
- key1
- key2:subkey2
'''
from __future__ import absolute_import
import os
import logging
# Set up logging
log = logging.getLogger(__name__)
__virtualname__ = 'extra_minion_data_in_pillar'
def __virtual__():
return __virtualname__
def ext_pillar(minion_id, pillar, include, extra_minion_data=None):
def get_subtree(key, source_dict):
'''
Returns a subtree corresponfing to the specified key.
key
Key. Supports the ':' notation (e.g. 'key:subkey')
source_dict
Source dictionary
'''
ret_dict = aux_dict = {}
subtree = source_dict
subkeys = key.split(':')
# Build an empty intermediate subtree following the subkeys
for subkey in subkeys[:-1]:
# The result will be built in aux_dict
aux_dict[subkey] = {}
aux_dict = aux_dict[subkey]
if not subkey in subtree:
# The subkey is not in
return {}
subtree = subtree[subkey]
if subkeys[-1] not in subtree:
# Final subkey is not in subtree
return {}
# Assign the subtree value to the result
aux_dict[subkeys[-1]] = subtree[subkeys[-1]]
return ret_dict
log.trace('minion_id = {0}'.format(minion_id))
log.trace('include = {0}'.format(include))
log.trace('extra_minion_data = {0}'.format(extra_minion_data))
data = {}
if not extra_minion_data:
return {}
if include == '<all>':
return extra_minion_data
data = {}
for key in include:
data.update(get_subtree(key, extra_minion_data))
return data