How can I share a (global) variable between salt states?

HOW TO -️ October 18, 2021

I am using Salt for deployment and configuration management. In order to attach to a FreeIPA server, I need to randomly generate an OTP token and then use it to attach to IPA. How can I share a variable between SLS files?

For example,

init.sls:

ERROR Rendering Code Block

./freeipa/example.sls:

ERROR Rendering Code Block

What I find is that everything looks good in example.sls, but I cannot reference this variable in init.sls. Do I need to be doing salt['random.get_str'](20) inside of init.sls instead? If so, how can I reference the otp variable in example.sls - or vice-versa - how can I reference the otp variable set in example.sls inside of init.sls?

Answer

I ended up just merging the two jinja files into one, but so far the answers have been really ugly hacks (seta grain or use pillar data, write out a temp file, etc.) Hopefully we can find something better than this.

Initializing...