Search code examples
erbpuppet

ERB template not picking up variables from hieradata


I have a puppet infrastructure and I've created a new module which refers to variables that exist in the hieradata yaml files, that all works fine in the manifest section. However, when I'm referring to them in an erb template the variables display nothing after the puppetrun. The file is there, just not the variables..

init.pp

class cms_nxlog ($msi_file) {
    anchor { 'cms_nxlog::begin': }
->
file { "C:/CMS/${cms_nxlog::msi_file}":
    ensure      =>  'file',
    source      =>  "puppet:///modules/cms_nxlog/${cms_nxlog::msi_file}",
    owner       => 'Administrators',
    group       => 'Users',
    mode        => '0770'

}
->
package { 'NXLOG-CE':
    ensure      =>  installed ,
    source      =>  "C:\\CMS\\${cms_nxlog::msi_file}",
}
->
file { "C:/Program Files (x86)/nxlog/conf/nxlog.conf":
    ensure      => 'file',
    content      => template('cms_nxlog/nxlog.conf.erb'),
    owner       => 'Administrators',
    group       => 'Users',
    mode        => '0770',
    notify      => Service['nxlog'],
}
->
service { 'nxlog' :
    ensure      =>  'running',
    require     => Package['NXLOG-CE']
}
->
    anchor { 'cms_nxlog::end': }
}

Relevant section of the erb template:

<Output out>
Module om_udp
   Host <%= scope.lookupvar('cms::log_server') %>
   Port <%= scope.lookupvar('cms_nxlog::port') %>
</Output>

Relevant sections of the yaml

cms_nxlog::msi_file:               nxlog-ce-2.8.1248.msi
cms_nxlog::port:                   514
cms::log_server:              192.168.1.50

The whole thing installs fine, it's just when copying the erb it seems to not fill in the content of the scope.lookupvar so I end up with

<Output out>
Module om_udp
   Host 
   Port 
</Output>

As I said previously the variables seem to work ok in the manifest, just not in the template. I've compared this to a similar module which seems to work to no avail.

Thanks


Solution

  • You need to call scope.function_hiera, not scope.lookupvar:

    <Output out>
    Moudule om_udp
        Host <%= scope.function_hiera(['cms::log_server']) %>
        Port <%= scope.function_hiera(['cms_nxlog::port']) %>
    </Output>
    

    Take note that function_hiera takes an array as it's argument, not a string.

    Hope this helps!