hiera, template and array

40 views
Skip to first unread message

Laci D

unread,
Mar 27, 2023, 4:21:09 PM3/27/23
to Puppet Users
I'm working on defining NTP servers from Hiera.

For Linux servers I have been using puppetlabs-ntp, which has been working nicely. Now I need to add support for FreeBSD. Above module doesn't support FreeBSD but I can edit ntp.conf with file resource type. 

This is where things got complicated, file adds extra ["..."] around the value form hiera, since it's an array. Array type is required for puppetlabs-ntp
Question is how can I get rid of the extra squarely braces and double quotes?
Rather than using a static file I'd like to stick to hiera since the ntp can very based on datacenter.

/etc/ntp.conf
server ["169.254.169.123"]

Desired /etc/ntp.conf
server 169.254.169.123

hieradata/site.yaml
ntp:
  servers:
    - 169.254.169.123


templates/ntp/ntp.conf.erb
server <%= @ntp['servers'] %>

Martin Alfke

unread,
Mar 28, 2023, 2:57:23 AM3/28/23
to puppet...@googlegroups.com
You must iterate as servers is an array:

<% $servers.each |$server| { -%>
server: <%= $server %>
<%- } -%>

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/848044f1-888d-44b3-8098-2c3569eb1608n%40googlegroups.com.

Laci D

unread,
Mar 28, 2023, 8:37:36 AM3/28/23
to Puppet Users
Thank you Martin!

I used your example and I think something is missing.
Since "servers" is under "ntp" in the hiera file (see example in my original email) maybe we need to define that in the erb file?


Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Internal Server Error: org.jruby.exceptions.SyntaxError: (SyntaxError) /etc/puppetlabs/code/modules/site/templates/ntp/ntp.conf.erb:6: syntax error, unexpected tSTRING_BEG
_erbout.<< "server: ".freeze; _erbout.<<((...
           ^
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

Dirk Heinrichs

unread,
Mar 28, 2023, 8:55:32 AM3/28/23
to puppet...@googlegroups.com
Am Dienstag, dem 28.03.2023 um 05:37 -0700 schrieb Laci D:

Since "servers" is under "ntp" in the hiera file (see example in my original email) maybe we need to define that in the erb file?

I think the example is slightly wrong. Should IMHO be "@servers" and then just "server" (without the leading "$").

HTH...

Dirk
-- 
Dirk Heinrichs
Senior Systems Engineer, Delivery Pipeline
OpenText ™ Discovery | Recommind
Recommind GmbH, Von-Liebig-Straße 1, 53359 Rheinbach
Vertretungsberechtigte Geschäftsführer Gordon Davies, Madhu Ranganathan, Christian Waida, Registergericht Amtsgericht Bonn, Registernummer HRB 10646
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden
Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail sind nicht gestattet.

Martin Alfke

unread,
Mar 28, 2023, 10:08:43 AM3/28/23
to puppet...@googlegroups.com
My example is an epp template.

What name does the variable have? $ntp? And: is this a hash with servers key set to an array?

In this case your erb (!) template can look like the following:

<% @ntp[’servers’].each { |server| -%>
server: <%= server %>
<% } -%>

Laci D

unread,
Mar 28, 2023, 10:30:04 AM3/28/23
to Puppet Users
I tried both epp and erb templates.

This is the manifest file:

class site::profiles::ntp {
    $ntp = hiera_hash('ntp')

    case $::operatingsystem {
            'freebsd': {
               file { "/etc/ntp.conf":
                   ensure  => file,
                   recurse => true,
                   purge   => true,
                   force   => true,
                   owner   => "root",
                   group   => 0,
                   mode    => "0644",
                   content => template('site/ntp/ntp.conf.epp'),
                   #content => template('site/ntp/ntp.conf.erb'),
               }
               service { 'ntpd':
                   ensure => 'running',
                   enable => true,
               }
               service { 'ntpdate':
                   enable => true,
               }
            }
        default: {

         class { 'ntp':
               servers => $ntp['servers'],
         }

       }
   }
}


The template:

templates/ntp/ntp.conf.epp
<% @ntp[’servers’].each { |server| -%>
server: <%= server %>
<% } -%>


And this is the hiera (as of now there is only one ntp server but there'll be more in the future):
ntp:
  servers:
    - 169.254.169.123


Martin with the latest change I'm getting:
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, Failed to parse template site/ntp/ntp.conf.epp:
  Filepath: /etc/puppetlabs/code/modules/site/templates/ntp/ntp.conf.epp
  Line: 3
  Detail: undefined local variable or method `’servers’' for #<Puppet::Parser::TemplateWrapper:0x34a1bb52>
 (file: /etc/puppetlabs/code/modules/site/manifests/profiles/ntp.pp, line: 14, column: 31) 

Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

Martin Alfke

unread,
Mar 29, 2023, 4:33:27 AM3/29/23
to puppet...@googlegroups.com
In EPP the template must look different:

<% $site::profiles::ntp::ntp[‘servers'].each |$server| { -%>
server: <%= $server %>
<% } -%>

An array may consist of a single entry only.

Laci D

unread,
Mar 29, 2023, 10:52:01 AM3/29/23
to Puppet Users
The following configuration is finally working, yay! 
I replaced "servers" with "ntp_host" in hiera to have a clean separation between "server" and "servers".

Thank you for the help!


templates/ntp/ntp.conf.erb
# File is managed by Puppet

<% if @ntp['ntp_host'] -%>
server <%= @ntp['ntp_host'].join("\nserver ") %> iburst
<% end -%>

restrict default limited kod nomodify notrap noquery nopeer
restrict source  limited kod nomodify notrap noquery
restrict -6 default ignore
restrict 127.0.0.1
restrict ::1

server 127.127.1.0
fudge 127.127.1.0 stratum 10
tos minclock 3 maxclock 6
driftfile /var/db/ntpd.drift

leapfile "/var/db/ntpd.leap-seconds.list"

manifests/profiles/ntp.pp
class site::profiles::ntp {
    $ntp = hiera_hash('ntp')

    case $::operatingsystem {
            'freebsd': {
               file { '/etc/ntp.conf':
                   ensure  => file,
                   recurse => true,
                   purge   => true,
                   force   => true,
                   owner   => "root",
                   group   => 0,
                   mode    => "0644",
                   content => template('site/ntp/ntp.conf.erb'),
                   notify  => Service['ntpd'],

               }
               service { 'ntpd':
                   ensure => 'running',
                   enable => true,
                   require => File['/etc/ntp.conf'],

               }
               service { 'ntpdate':
                   enable => true,
               }
            }
        default: {

         class { 'ntp':
               servers  => $ntp['ntp_host'],
         }

       }
   }

hieradata/site.yaml
ntp:
  ntp_host:
    - 169.254.169.123


Dan White

unread,
Mar 29, 2023, 4:44:21 PM3/29/23
to Puppet Users Mailing List
Silly question:  Why not use https://forge.puppet.com/modules/puppetlabs/ntp  ?
_______________________________________________________
Dan White : d_e_...@icloud.com
“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.” 
Bill Waterson (Calvin & Hobbes)

Laci D

unread,
Mar 30, 2023, 9:17:14 AM3/30/23
to Puppet Users
puppetlabs-ntp doesn't support FreeBSD.
I do use this module to manage ntp on Ubuntu.

Reply all
Reply to author
Forward
0 new messages