question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Grains are not fetched/refreshed within the current SALT Run

See original GitHub issue

Description of Issue

When you read some data and write it to a grain within the salt state and try to fetch the value of the grain within the same run the updated value are not available. The grains value will not be available within the same execution even after refresh_grains is fired. However, when the state is executed again the grains are fetched.

Setup

Here is the state where I set the grains and refresh it.

{% set role = salt['pillar.get']('roles') %}

s_pgs_set_role:
  grains.list_present:
    - name: db_roles
    - value: {{role}}

s_pgs_grains_sync:
  module.run:
  -  name: saltutil.refresh_grains

In the same state after refreshing the grain I have a If statement to check the value of db_roles.

{% set chk = salt['grains.get']('db_roles', 'no_val') %}

The value of the db_roles is never returned. It always get no_val. However, when I check /etc/salt/grains file. The grain is written there with the appropriate value.

And one point why I am writing from pillar to grain? That is because these are temporary pillars and will be cleared off after the execution. So I have to capture it in the grain as the first step.

Why is the grain value not fetched even though its updated?

Steps to Reproduce Issue

(Include debug logs if possible and relevant.)

Versions Report

Salt Version: Salt: 2018.3.0

Dependency Versions: cffi: 1.5.2 cherrypy: Not Installed dateutil: 2.4.2 docker-py: Not Installed gitdb: Not Installed gitpython: Not Installed ioflo: Not Installed Jinja2: 2.8 libgit2: Not Installed libnacl: Not Installed M2Crypto: 0.29.0 Mako: Not Installed msgpack-pure: Not Installed msgpack-python: 0.4.6 mysql-python: Not Installed pycparser: 2.10 pycrypto: 2.6.1 pycryptodome: Not Installed pygit2: Not Installed Python: 2.7.13 (default, Jan 11 2017, 10:56:06) [GCC] python-gnupg: Not Installed PyYAML: 3.12 PyZMQ: 14.0.0 RAET: Not Installed smmap: Not Installed timelib: Not Installed Tornado: 4.2.1 ZMQ: 4.0.4

System Versions: dist: SuSE 12 x86_64 locale: ANSI_X3.4-1968 machine: x86_64 release: 4.12.14-95.24-default system: Linux version: SUSE Linux Enterprise Server 12 x86_64

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
max-arnoldcommented, Nov 26, 2019

To apply the state, Salt needs to convert it from YAML format to a data structure. Since it is impossible to parse a YAML file that has Jinja statements inside, Salt needs to parse Jinja first.

In other words, the pipeline looks like this:

  1. Parse Jinja
  2. Parse the resulting YAML
  3. Interpret the data structure
  4. Apply the state

So, your {% set chk = salt['grains.get']('db_roles', 'no_val') %} statement is parsed on step 1, when the grain doesn’t exist yet.

0reactions
stale[bot]commented, Jan 19, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Salt grains - Salt user guide - Salt Project Documentation
This is called the grains interface, because it presents Salt with grains of information. Grains are collected for the operating system, domain name,...
Read more >
mrxpalmeiras - Salt Cheat Sheet - Google Sites
salt 'target' cmd.run 'start powershell "Restart-Service -Name salt-minion"' ... All grain data is stored on the minion in /etc/salt/grains file.
Read more >
Whole Grains | The Nutrition Source
Whole grains offer a “complete package” of health benefits, unlike refined grains, which are stripped of valuable nutrients during processing.
Read more >
Grains - MyPlate
What foods are in the Grains Group? Foods made from wheat, rice, oats, cornmeal, barley, or another cereal grain is a grain product....
Read more >
Existing Standards for Whole Grains
Only certain grain products qualify; no added sugar, salt, or oil allowed in rice, barley, bulgur or oatmeal; sugar restriction and iron requirement...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found