-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Accessing Grains in Custom Grains #47338
Comments
No dunders are injected into the grains modules except for Also, the problem with importing other grains, is that the best way to load them is through the loader, which would also try to load your custom grain, so you end up with the chicken and the egg scenario. The best way to do what you want to do, is write your own function to get the hostname or whatever value that you need, and use that in your custom grain, unfortunately there is just not a better way to logically get that value with the current loader system that we have. |
@gtmanfred : Thanks for the explanation. That makes sense. I guess for my example "hostname" above it works, because the function hostname is a relatively independent core grain. Still, according to the documentation there is a precedence to the order of how grains are loaded:
Thus, at the point in time when 4 is evaluated 1 to 3 must have already been evaluated. This matches (in part) with what I observed in the loader:
I think the code corresponds to 1 and 4 (I do not know how 2 and 3 are loaded). If one were to inject the existing grains into the second |
Do you have a link to where it says that in the documentation? because what actually happens afaik is that all of the grains are loaded by the loader, including custom grain modules, then all of them are run, and then that data is overwritten by /etc/salt/grains and grains in /etc/salt/minion All of the grain modules are loaded with this one function https://github.com/saltstack/salt/blob/2017.7/salt/loader.py#L624 Whether they are in They are all loaded, and it runs the function and assigns it to https://github.com/saltstack/salt/blob/2017.7/salt/loader.py#L722 You could try to just pass in Now, it does run all the https://github.com/saltstack/salt/blob/2017.7/salt/loader.py#L745 and then it mixes up the way we would write grains if it is not a core, and they wouldn't all be written the same. |
@saltstack/team-core does anyone have an opinion on passing the |
@gtmanfred, for sure: https://docs.saltstack.com/en/latest/topics/grains/#precedence Thanks for looking into this. I agree that this would require some testing. But looking at just the plain code it seems like the custom grains could easily get access to the core grains. Maybe I'll give it a shot locally for fun 🏎 |
we will definitely need to do some inspection into the grains modules, and make sure they accept arguments like we do for the proxy() modules, but if something accepts a |
@gtmanfred , I wrote a little spike to demonstrate how this could work:
I have not worked with salt proxies. The assumption is that if one parameter is passed we check if its proxy and then pass proxy. For any other parameter we would always pass the grains. If two parameters are passed we pass grain and proxy (assuming that grain would always be the first parameter). Otherwise we don't pass anything. Using this one could write the following custom grain:
Querying the grain leads to to this:
|
that looks great, but I would still say we should check if If you wouldn't mind opening a PR for this, and a test case, that would be greatly appreciated. Thanks, |
Actually, I got it. |
Test included #47372 . Awesome idea! Thanks, |
Closes saltstack#47338 Signed-off-by: Rares POP <[email protected]>
Is this feature released? It does not seem to be working for me, on salt version 2018.3.0
|
This grain is obviously copied from #47372 -- the exact test case specified there -- though it occurs with other function names/logic |
No, this is a new feature that will be in the Fluorine release.
…On Sat, Aug 11, 2018, 12:12 AM Johnny Dallas ***@***.***> wrote:
This grain is obviously copied from #47372
<#47372> -- the exact test case
specified there -- though it occurs with other function names/logic
—
You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
<#47338 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAssoe47rBEQe8Pb7NDU7oRJ5Vh2BvEhks5uPmeygaJpZM4Tm7Sk>
.
|
This is a question and possibly a feature request.
When writing custom grains I want to be able to utilize already existing grain information (either from core grains or other custom grains) in my custom grain. This is useful for situations when one property of a minion depends on another property of a minion that is already represented by another grain.
Example:
Let's assume we have a set of servers. Each server has a hostname that follows a specific naming convention. Part of that naming convention is that a role is encoded in the name of each server. To be able to target specific roles via grains one wants to retrieve the hostname of a minion, extract the role information and make it available as a grain.
One way of retrieving the hostname is via some custom python code in the custom grain. Another way is to e.g. just import
from salt.grains.core import hostname
and reuse this function.From my POV the second option is preferable; it is DRY and probably better maintained than any custom code I would write.
Thus my questions are:
Versions Report
The text was updated successfully, but these errors were encountered: