简体   繁体   中英

Puppet template variable inside another template variable

How do I call a variable from my manifest inside, while of another variable in a template.erb file?

This is what I've tried to do:

<%= food[<%= menu %>] %>

How do I get this to work?

You just use the variable, as is:

<%= food[menu] %>

You're already in "Ruby code space" within the ERB expansion, so you can use Ruby code as you normally would.

Referring to the Puppet 4.5 Embedded Ruby (ERB) template syntax section on Accessing Puppet Variables , there are 2 forms of variable access:

  • @variable syntax
  • scope['variable'] syntax

From the example in the question, there's not enough information to accurately determine the origin of the food variable. This question assumes that it's the result of processing an array or hash manifest variable. If food is the manifest variable, itself, it should be prefaced with @ , as such:

<%= @food[menu] %>

If the manifest variable is foods , and the food variable is an element of the enumerable (Array or Hash), it would be used as such:

<%= @foods.each do |food| %>
  <%= @food[menu] %>
<% end %>

Stick with the documented methods of accessing the manifest variable, and you can use it in your template exactly as you would within traditional Ruby code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM