Help with data structure

52 views
Skip to first unread message

Jonathan Gazeley

unread,
Feb 2, 2017, 6:00:23 AM2/2/17
to puppet...@googlegroups.com
Hi folks,

I'm grappling with a data structure that I want to model as a hash in
Puppet and ultimately mash it down to a string for use in a config file.

I'm writing out a config for Nagios BPI and there is one complex
parameter "members" which is described like this in the documentation:

# members - a comma delineated list of members.
# For services, format is: <hostname>;<servicename>;<opt>,
# For groups, format is: $<groupID>;<opt>,
# the <opt> is an '&' or '|' character.
# '&' option after host:servicename means service is part of a
CLUSTER
# For clusters, 'critical' is only reached when ALL services in a
cluster are NOT 'Ok'
# '|' option after host:servicename means it is an essential
service for the group,
# example: a 'critical' service with an '|' option will cause a
'critical' state
# for the entire group.

When configured, the parameter looks like this:

members=radius-local01.example.com;RADIUS;&,
radius-local02.example.com;RADIUS;&, radius-local03.example.com;RADIUS;&,

I want to model this in a hash like this:

$members = {
'radius-local01.example.com' => {
service => 'RADIUS',
opt => '&',
},
'radius-local02.example.com' => {
service => 'RADIUS',
opt => '&',
},
'radius-local03.example.com' => {
service => 'RADIUS',
opt => '&',
},
}

Is my design sensible, and how can I manipulate this data to generate a
string in the config file as above? I'm using Puppet Enterprise 2016.5
but I only upgraded from Puppet 3.8 a couple of weeks ago, so I'm still
new to the Puppet 4 syntax.

Thanks,
Jonathan

--
Jonathan Gazeley
Senior Systems Administrator
IT Services
University of Bristol

Robert

unread,
Feb 5, 2017, 6:13:10 AM2/5/17
to puppet...@googlegroups.com
Hi Jonathan, 

this erb template:

members=<% @members.each_pair do |key, value| -%>
<%= key -%>;<%= value['service'] -%>;<%= value['opt'] -%>,<% end -%>

gives you exactly the parameter you mentioned.

So if you define the variable $members in the pp file and a file resource with content => template('modulename/templatename.erb') 

Inside the template you simply iterate through the key, value pairs. You can access hash members with their key; square brackets are used for accessing.

Sorry for the late answer, you probably figured it out already yourself :)

Best
Rp



--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/7794f367-7028-0bb5-422c-2d46bc133545%40bristol.ac.uk.
For more options, visit https://groups.google.com/d/optout.

Jonathan Gazeley

unread,
Feb 6, 2017, 9:47:48 AM2/6/17
to puppet...@googlegroups.com
Thanks for your reply. I'm still having problems with this :( This is my
worked example:

# Provide example config
uob_nagios::bpiconfig { 'radius':
displayname => 'RADIUS eduroam local',
primary => 1,
members => {
'radius01.example.com' => {
service => 'RADIUS',
opt => '&',
},
'radius02.example.com' => {
service => 'RADIUS',
opt => '&',
},
'radius03.example.com' => {
service => 'RADIUS',
opt => '&',
},
},
warning_threshold => 0,
critical_threshold => 0,
priority => 1,
}

# Defined type
define uob_nagios::bpiconfig (
$displayname,
$members,
$desc = undef,
$primary = 1,
$info = undef,
$warning_threshold = 0,
$critical_threshold = 0,
$priority = 1,
) {
concat::fragment{ "bpi-${title}":
target => '/usr/share/nagios/nagiosbpi/nagiosbpi/bpi.test.conf',
content => template('uob_nagios/bpi.conf.erb'),
order => '20',
}
}

# bpi.conf.erb
define <%= @title %> {
title=<%= @displayname %>
desc=<%= @desc %>
primary=<%= @primary %>
info=<%= @info %>
members=<% @members.each_pair do |key, value| -%><%= key
-%>;<%= value['service'] -%>;<%= value['opt'] -%>,<% end -%>
warning_threshold=<%= @warning_threshold %>
critical_threshold=<%= @critical_threshold %>
priority=<%= @priority %>
}

Running this gives the error:

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 uob_nagios/bpi.conf.erb:
Filepath:
/etc/puppetlabs/code/environments/367_bpi_config/local_modules/uob_nagios/templates/bpi.conf.erb
Line: 6
Detail: undefined local variable or method `members' for
#<Puppet::Parser::TemplateWrapper:0x6275a5c3>
at
/etc/puppetlabs/code/environments/367_bpi_config/local_modules/uob_nagios/manifests/bpiconfig.pp:34:16
at
/etc/puppetlabs/code/environments/367_bpi_config/local_modules/uob_nagios/manifests/bpi.pp:26
on node monitor.resnet.bris.ac.uk
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

I can't see anything wrong with the 'members' variable. Am I missing
something?

Thanks,
Jonathan


On 05/02/17 11:13, Robert wrote:
> Hi Jonathan,
>
> this erb template:
>
> members=<% @members.each_pair do |key, value| -%>
> <%= key -%>;<%= value['service'] -%>;<%= value['opt'] -%>,<% end -%>
>
> gives you exactly the parameter you mentioned.
>
> So if you define the variable $members in the pp file and a file
> resource with content => template('modulename/templatename.erb')
>
> Inside the template you simply iterate through the key, value pairs. You
> can access hash members with their key; square brackets are used for
> accessing.
> https://docs.puppet.com/puppet/latest/lang_data_hash.html#accessing-values
>
> Sorry for the late answer, you probably figured it out already yourself :)
>
> Best
> Rp
>
>
>
> On Thu, Feb 2, 2017 at 12:00 PM, Jonathan Gazeley
> <Jonathan...@bristol.ac.uk <mailto:Jonathan...@bristol.ac.uk>>
> wrote:
>
> Hi folks,
>
> I'm grappling with a data structure that I want to model as a hash
> in Puppet and ultimately mash it down to a string for use in a
> config file.
>
> I'm writing out a config for Nagios BPI and there is one complex
> parameter "members" which is described like this in the documentation:
>
> # members - a comma delineated list of members.
> # For services, format is: <hostname>;<servicename>;<opt>,
> # For groups, format is: $<groupID>;<opt>,
> # the <opt> is an '&' or '|' character.
> # '&' option after host:servicename means service is part of
> a CLUSTER
> # For clusters, 'critical' is only reached when ALL services in a
> cluster are NOT 'Ok'
> # '|' option after host:servicename means it is an essential
> service for the group,
> # example: a 'critical' service with an '|' option will cause
> a 'critical' state
> # for the entire group.
>
> When configured, the parameter looks like this:
>
> members=radius-local01.example.com
> <http://radius-local01.example.com>;RADIUS;&,
> radius-local02.example.com
> <http://radius-local02.example.com>;RADIUS;&,
> radius-local03.example.com <http://radius-local03.example.com>;RADIUS;&,
>
> I want to model this in a hash like this:
>
> $members = {
> 'radius-local01.example.com <http://radius-local01.example.com>' => {
> service => 'RADIUS',
> opt => '&',
> },
> 'radius-local02.example.com <http://radius-local02.example.com>' => {
> service => 'RADIUS',
> opt => '&',
> },
> 'radius-local03.example.com <http://radius-local03.example.com>' => {
> service => 'RADIUS',
> opt => '&',
> },
> }
>
> Is my design sensible, and how can I manipulate this data to
> generate a string in the config file as above? I'm using Puppet
> Enterprise 2016.5 but I only upgraded from Puppet 3.8 a couple of
> weeks ago, so I'm still new to the Puppet 4 syntax.
>
> Thanks,
> Jonathan
>
> --
> Jonathan Gazeley
> Senior Systems Administrator
> IT Services
> University of Bristol
>
> --
> 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
> <mailto:puppet-users%2Bunsu...@googlegroups.com>.
> <https://groups.google.com/d/msgid/puppet-users/7794f367-7028-0bb5-422c-2d46bc133545%40bristol.ac.uk>.
> For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
>
> --
> 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
> <mailto:puppet-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/puppet-users/CANwwCtz3_mWoMzk%3DQmN2jmCn2G8ce8hYp%2BMcvmW%2BNF2F4Ax6dg%40mail.gmail.com
> <https://groups.google.com/d/msgid/puppet-users/CANwwCtz3_mWoMzk%3DQmN2jmCn2G8ce8hYp%2BMcvmW%2BNF2F4Ax6dg%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


Jonathan Gazeley

unread,
Feb 6, 2017, 10:34:07 AM2/6/17
to puppet...@googlegroups.com
Sorry, this is my fault. I hadn't done "git push" to deploy my new code
before testing :( Your example works perfectly!

Thanks,
Jonathan
Reply all
Reply to author
Forward
0 new messages