mirror of
https://github.com/valitydev/salt.git
synced 2024-11-06 08:35:21 +00:00
Allow for passing in previously compiled grains to custom grain modules
Closes #47338
This commit is contained in:
parent
1de101b8ab
commit
05ab624fcd
@ -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
|
||||
==================
|
||||
|
@ -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
|
||||
-----------------------------------------
|
||||
|
||||
|
@ -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:
|
||||
|
@ -0,0 +1,4 @@
|
||||
def test(grains):
|
||||
if 'os' in grains:
|
||||
return {'custom_grain_test': 'itworked'}
|
||||
return {'custom_grain_test': 'itdidntwork'}
|
22
tests/integration/grains/test_custom.py
Normal file
22
tests/integration/grains/test_custom.py
Normal 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')
|
Loading…
Reference in New Issue
Block a user