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
On Wednesday, March 29, 2023 at 4:33:27 AM UTC-4 Martin Alfke wrote:
> 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.
>
> On 28. Mar 2023, at 16:30, Laci D <[email protected]> wrote:
>
> 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 <[email protected]> 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 <[email protected]> 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 [email protected].
>>> 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 [email protected].
>>
>> 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 [email protected].
>
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/b1ef24bc-7995-469f-b81c-a0f51c4010c9n%40googlegroups.com
>
> <https://groups.google.com/d/msgid/puppet-users/b1ef24bc-7995-469f-b81c-a0f51c4010c9n%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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-users/e3bc6f45-943f-4846-b513-cdf601caf31en%40googlegroups.com.