If you use the grain/state pattern a lot, or if you use external pillars you’ve probably stumbled upon a limitation with grains and pillars.
During a Salt run, if you set a grain, or update an external pillar, it won’t be reflected in the grains and pillars found in the grains and pillar dictionaries. This is because you’ve updated it, but it hasn’t been reloaded into the in-memory data structures that salt creates at the beginning of the run. From a performance point of view this is good, since reloading grains and especially loading external pillars is quite slow.
To fix this limitation, I added a PR to add the global state arguments reload_grains and reload_pillar. These work similar to reload_modules (and in fact, they imply reload_modules).
For example, if you’re using the etcd external pillar, the following will now work:
Ensure etcd key exist for host:
module.run:
- name: etcd.set
- key: /myorg/servers/myhost
- value: {{ grains['ip_interfaces']['eth0'][0] }}
- profile: etcd_profile
- reload_pillar: True
Ensure example file has pillar contents:
file.managed:
- name: /tmp/test.txt
- contents_pillar: servers:myhost
Note, though, that jinja in sls files is executed before the states are executed, so this will still not work:
Ensure etcd key exist for host:
module.run:
- name: etcd.set
- key: /myorg/servers/myhost
- value: {{ grains['ip_interfaces']['eth0'][0] }}
- profile: etcd_profile
- reload_pillar: True
Ensure example file has pillar contents:
file.managed:
- name: /tmp/test.txt
- contents: {{ pillar.servers.myhost }}
Jinja used in template files is executed along with the state in order, so that’ll work without issue.
This change is merged into the develop branch of Salt, and will be included in the first stable release of 2015 (Lithium).