Grains are not fetched/refreshed within the current SALT Run
See original GitHub issueDescription 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:
- Created 4 years ago
- Comments:7 (2 by maintainers)
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:
So, your
{% set chk = salt['grains.get']('db_roles', 'no_val') %}
statement is parsed on step 1, when the grain doesn’t exist yet.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.