fix grains filter_by function when using salt-ssh

very similar to #18238 and 7975b0a11
This commit is contained in:
Sławek Ehlert 2014-11-19 12:25:32 +01:00
parent 873d1c1803
commit 70b435d4b5

View File

@ -5,6 +5,7 @@ Return/control aspects of the grains data
from __future__ import absolute_import
# Import python libs
import collections
import math
# Import salt libs
@ -140,7 +141,7 @@ def ls(): # pylint: disable=C0103
return sorted(__grains__)
def filter_by(lookup_dict, grain='os_family', merge=None):
def filter_by(lookup_dict, grain='os_family', merge=None, default='default'):
'''
.. versionadded:: 0.17.0
@ -156,7 +157,7 @@ def filter_by(lookup_dict, grain='os_family', merge=None):
{% set apache = salt['grains.filter_by']({
'Debian': {'pkg': 'apache2', 'srv': 'apache2'},
'RedHat': {'pkg': 'httpd', 'srv': 'httpd'},
}) %}
}), default='Debian' %}
myapache:
pkg:
@ -200,16 +201,36 @@ def filter_by(lookup_dict, grain='os_family', merge=None):
values for non-standard package names such as when using a different
Python version from the default Python version provided by the OS
(e.g., ``python26-mysql`` instead of ``python-mysql``).
:param default: default lookup_dict's key used if the grain does not exists
or if the grain value has no match on lookup_dict.
.. versionadded:: 2014.1.0
CLI Example:
.. code-block:: bash
salt '*' grains.filter_by '{Debian: Debheads rule, RedHat: I love my hat}'
# this one will render {D: {E: I, G: H}, J: K}
salt '*' grains.filter_by '{A: B, C: {D: {E: F,G: H}}}' 'xxx' '{D: {E: I},J: K}' 'C'
'''
ret = lookup_dict.get(__grains__[grain], None)
ret = lookup_dict.get(
__grains__.get(
grain, default),
lookup_dict.get(
default, None)
)
if merge:
salt.utils.dictupdate.update(ret, merge)
if not isinstance(merge, collections.Mapping):
raise SaltException('filter_by merge argument must be a dictionary.')
else:
if ret is None:
ret = merge
else:
salt.utils.dictupdate.update(ret, merge)
return ret