Puppet passes data to templates viaspecial objects and variables, which you can use in the tagged Ruby code to control the templates' output. The followingexample shows non-printing tags ( <%), expression-printing tags ( <%=), and comment tags ( <%#). A hyphen in a tag ( -) strips leading or trailingwhitespace when printing the evaluatedtemplate: <% if @keysenable -%<%# Printing the keys file, trusted key, request key, and control key: -%keys <%= @keysfile%<% unless @keystrusted.empty? -%trustedkey <%= @keystrusted.join(' ')%<% end -%<% if @keysrequestkey!= ' -%requestkey <%= @keysrequestkey%<% end -%<% if @keyscontrolkey!= ' -%controlkey <%= @keyscontrolkey%<% end -%<% end -% ERB tags.
The following table provides an overview of the main tag types used withERB. See the sections below for additional detail about each tag, including instructions ontrimming whitespace and escaping special characters. I want to.EPP tag syntaxInsert the value of a single expression.<%= EXPRESSION%Execute an expression without inserting avalue.<% EXPRESSION%Add a comment.<%# COMMENT%Text outside a tag is treated as literal text, but is subject to any tagged Ruby code surrounding it. For example, text surrounded by atagged if statement onlyappears in the output if the condition is true. Expression-printing tags. @variable. scope'variable' and its older equivalent, scope.lookupvar('variable')@variableAll variables in the current scope (including global variables) arepassed to templates as Ruby instance variables, whichbegin with “at” signs ( @).
If you can access a variable by its short name in the surroundingmanifest, you can access it in the template by replacing its $ sign with an @, so that $os becomes @os, and $trusted becomes @trusted.This is the most legible way to access variables, but it doesn’tsupport variables from other scopes. For that, you need to use the scope object.
Zacchaeus Song Lyrics
Scope'variable' or scope.lookupvar('variable')Puppet also passes templates an objectcalled scope,which can access all variables (including out-of-scope variables) with a hash-styleaccess expression. For example, to access $ntp::tinker you would use scope'ntp::tinker'.Another way to use the scope object is to call its lookupvar method and pass thevariable’s name as its argument, as in scope.lookupvar('ntp::tinker'). This is exactly equivalent tothe above, if slightly less convenient. This usage predates the hash-style indexingadded in Puppet 3.0.
Puppet data types in Ruby. End statementin Ruby lets you write conditional text. Put the controlstatements in non-printing tags, and the conditional text between the tags:<% if Other code.end Using iterationRuby lets you iterate over arrays and hashes withthe each method.This method takes a block of code and executes it one time for each element in the array orhash. In a template, untagged text is treated as part of the code that gets repeated. Youcan think of literal text as an instruction, telling the evaluator to insert that textinto the final output. ERB Templates for DummiesBasic code: require 'erb'ERB.new(template).result bindingfortemplateWhat are template and bindingfortemplate? TemplateJust the template content. You can read it from a file just with a File.read(path). Binding for templateAencapsulate the execution context at some particular place in the code and retain this context for future use.How do you use it? Easy: def bindingformytemplatedevices = 'eth0', 'br0'ipaddrs = '192.168.12.166', '192.168.12.199'netmasks = '255.255.255.0', '255.255.255.0'hwaddrs = 'networks = 'gateways = '192.168.12.254', '192.168.12.204'bindingendThat will return a binding with all six arrays (I changed hwaddr and network to arrays for consistency. Iterating over arraysThe usual way is using the each method, just like this: <%- devices.each do d %auto <%= d% inet static<%- end%but there are other methods if you wanna put all in one line, for example devices.join ' ' will join all the strings with a space in between.Puppet 5.5 Templates