Skip to content
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

The current documented grains precedence is unusable in certain cases for custom grains #29166

Closed
edgan opened this issue Nov 24, 2015 · 3 comments
Labels
Bug broken, incorrect, or confusing behavior Core relates to code central or existential to Salt Grains P3 Priority 3 severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around stale
Milestone

Comments

@edgan
Copy link
Contributor

edgan commented Nov 24, 2015

I want to set a grain in one state, reload_grains: True, do some stuff in the middle, and then change the same grain in another state. I use some jinja below to reload the grain, and set a variable in the state. But the rendering of the code before execution checks to see if the grain exists, doesn't find it, and throws an error.

So a grain set from within the state doesn't apply. I tried to initialize the grain with _grains, but with the current precedence, it always overrides my in state grains. The current precedence is inconsistent between core grains and custom grains.

Sane precedence:

_grains become before /etc/salt/grains instead of after

Documented precedence:

https://docs.saltstack.com/en/latest/topics/targeting/grains.html#precedence


Core grains.
Custom grains in /etc/salt/grains.
Custom grains in /etc/salt/minion.
Custom grain modules in _grains directory, synced to minions.

Error:

/var/cache/salt/minion/files/base/baseline/map.jinja(50):

---
[...]
    'mirror': 'mirror.foo.com'
  },
}
, grain="mirror", merge=salt['pillar.get']('baseline')) %}

{% do defaults.baseline.update(mirror_map) %}    <======================

{% set settings = salt['pillar.get']('baseline', default=defaults.baseline, merge=True) %}

---
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/salt/utils/templates.py", line 368, in render_jinja_tmpl
    output = template.render(**decoded_context)
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "<template>", line 1, in top-level template code
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 1013, in make_module
    return TemplateModule(self, self.new_context(vars, shared, locals))
  File "/usr/lib/python2.7/site-packages/jinja2/environment.py", line 1070, in __init__
    self._body_stream = list(template.root_render_func(context))
  File "/var/cache/salt/minion/files/base/baseline/map.jinja", line 50, in top-level template code
    {% do defaults.baseline.update(mirror_map) %}
TypeError: 'NoneType' object is not iterable

Jinja:

{% set mirror_map = salt['grains.filter_by']({
  False: {
    'mirror': 'mirror.example.com'
  },
  True: {
    'mirror': 'mirror.foo.com'
  },
}
, grain="mirror", merge=salt['pillar.get']('baseline')) %}
@jfindlay jfindlay added Bug broken, incorrect, or confusing behavior severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around Core relates to code central or existential to Salt P3 Priority 3 Grains labels Nov 25, 2015
@jfindlay jfindlay added this to the Approved milestone Nov 25, 2015
@jfindlay
Copy link
Contributor

@edgan, thanks for the report. These other issues also discuss the precedence issue: #27005, #19611, #16268.

@skoenig
Copy link

skoenig commented Oct 6, 2016

Seems like due to restructuring the precedence of grains documentation has been moved from the 'targeting' page to https://docs.saltstack.com/en/latest/topics/grains/#precedence since this issue was opened. As @jfindlay mentioned, the documentation issue was also addressed in #27005 which was resolved then by #27706 - at least for the 2015.5 release.
I experienced in salt 2016.3.3 (Boron) that the last two instances in the precedence list are not correct:

  1. Custom grains in /etc/salt/minion.
  2. Custom grain modules in _grains directory, synced to minions.

File-based grains in /etc/salt/minion definitely override grain modules in the _grains module. So for me it seems the precedence section should be reverted to a067e6c. Should I open another issue or is this related enough?

@stale
Copy link

stale bot commented Feb 7, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

If this issue is closed prematurely, please leave a comment and we will gladly reopen the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug broken, incorrect, or confusing behavior Core relates to code central or existential to Salt Grains P3 Priority 3 severity-medium 3rd level, incorrect or bad functionality, confusing and lacks a work around stale
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants