Allow for passing in previously compiled grains to custom grain modules

Closes #47338
This commit is contained in:
Daniel Wallace 2018-04-27 15:22:38 -05:00
parent 1de101b8ab
commit 05ab624fcd
No known key found for this signature in database
GPG Key ID: 5FA5E5544F010D48
5 changed files with 49 additions and 2 deletions

View File

@ -269,6 +269,11 @@ grain will override that core grain. Similarly, grains from
``/etc/salt/minion`` override both core grains and custom grain modules, and
grains in ``_grains`` will override *any* grains of the same name.
For custom grains, if the function takes an argument ``grains``, then the
previously rendered grains will be passed in. Because the rest of the grains
could be rendered in any order, the only grains that can be relied upon to be
passed in are ``core`` grains. This was added in the Fluorine Release.
Examples of Grains
==================

View File

@ -5,6 +5,16 @@ Salt Release Notes - Codename Fluorine
======================================
Grains Dictionary Passed into Custom Grains
-------------------------------------------
Starting in this release, if a custom grains function accepts a variable named
``grains``, the Grains dictionary of the already compiled grains will be passed
in. Because of the non deterministic order that grains are rendered in, the
only grains that can be relied upon to be passed in are ``core.py`` grains,
since those are compiled first.
"Virtual Package" Support Dropped for APT
-----------------------------------------

View File

@ -751,8 +751,14 @@ def grains(opts, force_refresh=False, proxy=None):
# proxymodule for retrieving information from the connected
# device.
log.trace('Loading %s grain', key)
if funcs[key].__code__.co_argcount == 1:
ret = funcs[key](proxy)
parameters = list(funcs[key].__code__.co_varnames)
if funcs[key].__code__.co_argcount > 0:
kwargs = {}
if 'proxy' in parameters:
kwargs['proxy'] = proxy
if 'grains' in parameters:
kwargs['grains'] = grains_data
ret = funcs[key](**kwargs)
else:
ret = funcs[key]()
except Exception:

View File

@ -0,0 +1,4 @@
def test(grains):
if 'os' in grains:
return {'custom_grain_test': 'itworked'}
return {'custom_grain_test': 'itdidntwork'}

View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
'''
Test the core grains
'''
# Import python libs
from __future__ import absolute_import, unicode_literals
# Import Salt Testing libs
from tests.support.case import ModuleCase
class TestGrainsCore(ModuleCase):
'''
Test the core grains grains
'''
def test_grains_passed_to_custom_grain(self):
'''
test if current grains are passed to grains module functions that have a grains argument
'''
self.assertEqual(self.run_function('grains.get', ['custom_grain_test']), 'itworked')