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
On Tuesday, March 28, 2023 at 10:08:43 AM UTC-4 Martin Alfke wrote:

> 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 %>
> <% } -%>
>
> On 28. Mar 2023, at 14:37, Laci D <lac...@gmail.com> wrote:
>
> 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
>
> On Tuesday, March 28, 2023 at 2:57:23 AM UTC-4 Martin Alfke wrote:
>
>> You must iterate as servers is an array:
>>
>> <% $servers.each |$server| { -%>
>> server: <%= $server %>
>> <%- } -%>
>>
>> On 27. Mar 2023, at 22:21, Laci D <lac...@gmail.com> wrote:
>>
>> 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'] %>
>>
>> -- 
>> 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
>>  
>> <https://groups.google.com/d/msgid/puppet-users/848044f1-888d-44b3-8098-2c3569eb1608n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>>
>>
> -- 
> 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/1d68b2ef-0c4f-425f-a84d-3cc22e6f750an%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/puppet-users/1d68b2ef-0c4f-425f-a84d-3cc22e6f750an%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/b1ef24bc-7995-469f-b81c-a0f51c4010c9n%40googlegroups.com.

Reply via email to