Using a puppet hash for epp templates

I have the following code in my erb template:

<% if @proxy_cache_path.is_a?(Hash) -%>
<% @proxy_cache_path.sort_by{|k,v| k}.each do |key,value| -%>
  proxy_cache_path        <%= key %> keys_zone=<%= value %> levels=<%= @proxy_cache_levels %> max_size=<%= @proxy_cache_max_size %> inactive=<%= @proxy_cache_inactive -%>
<% end -%>

      

How to port it for epp template? I have very little information. Please, help.

+1


source to share


1 answer


Here's how you can do it:

Show an example class and how to declare an ERB and EPP template for comparison:

# manifests/init.pp
class foo () {
  $proxy_cache_path = {
    'apples'  => 1,
    'bananas' => 2,
  }
  $proxy_cache_levels = 2
  $proxy_cache_max_size = 2
  $proxy_cache_inactive = 2

  # Showing use of ERB:
  file { '/foo':
    ensure  => file,
    content => template('foo/mytemplate.erb')
  }

  # Showing use of EPP, which requires an explicit parameters hash:
  file { '/bar':
    ensure  => file,
    content => epp('foo/mytemplate.epp', {
      'proxy_cache_path'     => $proxy_cache_path,
      'proxy_cache_levels'   => $proxy_cache_levels,
      'proxy_cache_max_size' => $proxy_cache_max_size,
      'proxy_cache_inactive' => $proxy_cache_inactive,
    }),
  }
}

      

Fixed * contents of ERB file for comparison:

# templates/mytemplate.erb     
<% if @proxy_cache_path.is_a?(Hash) -%>
<% @proxy_cache_path.sort_by{|k,v| k}.each do |key,value| -%>
  proxy_cache_path        <%= key %> keys_zone=<%= value %> levels=<%= @proxy_cache_levels %> max_size=<%= @proxy_cache_max_size %> inactive=<%= @proxy_cache_inactive -%>
<% end -%>
<% end -%>

      

(* The code in the question is missing a closure end

.)

EPP file content:

# templates/mytemplate.epp 
<%- | Hash[String, Integer] $proxy_cache_path, Integer $proxy_cache_levels, Integer $proxy_cache_max_size, Integer $proxy_cache_inactive | -%>
<% include stdlib -%>
<% $proxy_cache_path.keys.sort.each |$key| { -%>
  proxy_cache_path        <%= $key %> keys_zone=<%= $proxy_cache_path[$key] %> levels=<%= $proxy_cache_levels %> max_size=<%= $proxy_cache_max_size %> inactive=<%= $proxy_cache_inactive -%>
<% } -%>

      

Notes on the contents of the EPP template file:



1) Parameters and their types are defined in the first line of the template. Using this line is optional, but good practice.

2) Since we declared the types in the first line, there is no need and redundant to check if the $proxy_cache_path

hash is.

3) We need to include stdlib to access functions keys

and sort

. This differs from Ruby (ERB) where these methods are built into the language.

4) I have simplified the code regarding Ruby (ERB) because Puppet (EPP) has no function sort_by

and there is really no need to use it in the ERB, which can be rewritten as:

<% if @proxy_cache_path.is_a?(Hash) -%>
<%   @proxy_cache_path.sort.each do |key,value| -%>
  proxy_cache_path        <%= key %> keys_zone=<%= value %> levels=<%= @proxy_cache_levels %> max_size=<%= @proxy_cache_max_size %> inactive=<%= @proxy_cache_inactive -%>
<%   end -%>
<% end -%>

      

Finally, some tests:

# spec/classes/test_spec.rb:
require 'spec_helper'

describe 'foo', :type => :class do
  it 'content in foo should be the same as in bar' do
    foo = catalogue.resource('file', '/foo').send(:parameters)[:content]
    bar = catalogue.resource('file', '/bar').send(:parameters)[:content]
    expect(foo).to eq bar
  end
end

      

And the tests are passing.

See the docs here .

+2


source







All Articles