Variables across snippets and templates

I have a 1.23RC1 test foreman instance set up and my snippets are not working quite the same. It seems that “global” variables prefaced with @ are no longer available (other than the built-in ones)

I have several variables that are generated by the parsing the hostname of the server being built, as well as from various host/subnet/global parameters in foreman. Is there a better way to do this?

For example, I set a variable “@app” in a snippet, and need to use it in my provisioning template, as well as another snippet called later in that same provisioning template. This works in 1.19 but no longer does. Safe mode is disabled. Any pointers, or documentation about how to get around this?

Thanks

Bill

Hello,

we have vastly redesigned the template engine, contexts are now pretty different. Using @xxx for “global” variables was a bad idea, you should have pass variables to snippets using the clean way via variables parameter. This is the full definition of the macro:

snippet(name, options = {}, variables: {})

If you want to pass variable called xxx do this:

snippet("my_snippet", {}, {xxx: "some value"})

Then use the variable in the snippet just like a regular variable:

<%= xxx %>

Thanks Lukas. The issue was that I had a snippet that calculated values for some variables that other snippets needed to access. I did that to keep the code out of the main provisioning template. Much of what I did in the past can go into subnet, location or os-specific parameters. The last couple need to be calculated from the hostname (and not entered in as parameters by the user)
I guess I can put that code in the provisioning template and just pass the necessary variables to the snippets that need them.

@sirinek, that sounds as cleaner approach, I can’t verify atm, but IIRC it behaves slightly differently than in last @lzap’s snippet, instead of <%= xxx %> you need to use <%= @xxx %>. If you look at kickstart network interface snippets, you’ll have plenty of examples.

Yeah, probably. One way or the other will work.

1 Like