Write arrays to an erb

75 views
Skip to first unread message

Sergiu Cornea

unread,
Oct 7, 2015, 12:12:23 PM10/7/15
to Puppet Users
Hello guys,

I am passing 4 arrays in the Hiera file a: {a,b,c} b: {d,e,f} c: {g,h,i} d: {j,k,l} which I am using them in an .erb template. However, when I am trying to run Puppet it is complaining saying that cannot associate the file with that name as it is already declared in a manifest.

I am using the create_resource function with Puppet 3.7 on Ubuntu 14.04.

Could someone please help me understand what I am doing wrong?

Thank you,

Regards,
Sergiu

Martin Alfke

unread,
Oct 7, 2015, 3:39:56 PM10/7/15
to puppet...@googlegroups.com
Hi Sergiu,
It would be useful to have the code pasted in gist.
Otherwise it will be difficult to help.

Best,
Martin

>
> Thank you,
>
> Regards,
> Sergiu
>
> This message and its attachments are private and confidential. If you have received this message in error, please notify the sender and remove it and its attachments from your system.
>
> The University of Westminster is a charity and a company limited by guarantee. Registration number: 977818 England. Registered Office: 309 Regent Street, London W1B 2UW.
>
>
> --
> 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/f2795f58-5d39-4ff3-9a44-79d2caeeae49%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Sergiu Cornea

unread,
Oct 8, 2015, 4:38:55 AM10/8/15
to Puppet Users
Hi Martin, 

Thank you for your response.

This is the code I have developed: 


Thank you,

Kind regards,
Sergiu

jcbollinger

unread,
Oct 8, 2015, 10:37:25 AM10/8/15
to Puppet Users


On Thursday, October 8, 2015 at 3:38:55 AM UTC-5, Sergiu Cornea wrote:
Hi Martin, 

Thank you for your response.

This is the code I have developed: 



Are we to assume that the commented-out File declaration in that code is the one that causes the difficulty?  So that the error occurs with the definition below?

define omd::omd_create::omd_rules (
  $omd_site    
= '',
  $omd_service
= [],
  $omd_ip      
= [],
  $h          
= {}, ) {
 
  file
{ $title:
    path    
=> '/omd/sites/test/etc/check_mk/multisite.d/wato/bi.mk',
   
ensure  => file,
    content
=> template('omd/bi.mk.erb'),
 
}
}

That would be completely believable.  Although the definition is not inherently erroneous, it only accommodates a single instance, because it declares a file resource with a fixed path, and any second instance will therefore attempt to declare a duplicate resource.

It's unclear what you're trying to do here.  If you are trying to manage separate files then of course you need to specify each one's name.  If you are trying to manage a single file with information gleaned from multiple resources, then then consider looking into the Concat module.  On the other hand, if you are indeed trying to manage a single file, then consider whether you can instead do it via a single resource -- it appears that your code could be modified to accommodate that, but it depends to some extent on how it is used, and what you want to do with it in the future.


John

Sergiu Cornea

unread,
Oct 8, 2015, 10:52:29 AM10/8/15
to Puppet Users
Hi John,

Thank you for your response.

Well what I am trying to do is to create A file with data from Hiera.

I am using this module: https://github.com/GRIF-IRFU/puppet-omd plus the one I have just developed to add new sites, hosts and commands. However, I have been asked to create Business Intelligence rules which takes some IP addresses and the same number of services and group them together. 

Let's say, I've got 10 nodes with 10 services each. In the bi.mk file I have to declare a rule which looks as follows:

aggregation_rules["customer"] = {
 'aggregation': 'worst!1!2',
 'comment': u'All the services associated with the customer',
 'nodes': [(u'10.0.0.1', u'service1_ssl'),
           (u'10.0.0.2',
            u'service1_101'),
           (u'10.0.0.3',
            u'service1_103')],
 'params': [],
 'title': u'Customer Services'
}

So therefore, what I am trying to do, is that I've got a template which does this for me, and I have specified A CLASS instead of define as you've said I was getting an error saying that the couldn't associate alias because file it is already declared (because of its path). So, the class i've created makes a file from an erb template. However, when I am running Puppet I just get printed in the file the name of the class instead of the data.

I was thinking about using concat but I am a Puppet beginner and I am not really sure how to go about incorporating the erb logic in the concat function.

Thank you,

Kind regards,
Sergiu

jcbollinger

unread,
Oct 9, 2015, 10:31:55 AM10/9/15
to Puppet Users


On Thursday, October 8, 2015 at 9:52:29 AM UTC-5, Sergiu Cornea wrote:
 
So therefore, what I am trying to do, is that I've got a template which does this for me, and I have specified A CLASS instead of define as you've said I was getting an error saying that the couldn't associate alias because file it is already declared (because of its path). So, the class i've created makes a file from an erb template. However, when I am running Puppet I just get printed in the file the name of the class instead of the data.



It appears, then, that the problem is centered around your template.  If you have not already done so, you should read the templating docs.

If you are asking specifically about the resulting of outputting the value of variable 'title', then yes, it will evaluate to the name of the class or title of the defined type from which the template is called.  Although using that might make sense if you're calling from a defined type, it is unlikely to make sense if you call from a class.  In the latter case, you'll need to provide the data you want to plug in there via a different variable.  If you want to output multiple rules, then perhaps you need an array or hash describing them and their per-rule data.  It looks like the hash obtained via hiera('omd_rules') (against the data presented in your pastebin snippet) might be exactly what you need there.

If you rename the hiera key to 'omd::omd_rules::rules' then maybe something along these lines might work:

modules/omd/manifests/omd_rules.pp:

class omd::omd_rules ($rules) {
  file
{ '/omd/sites/test/etc/check_mk/multisite.d/wato/bi.mk':
   
ensure  => 'file',
    content
=> template('omd/bi.mk.erb'),
 
}
}


modules/omd/templates/bi.mk.erb:

<% @rules.each { |rule, props| -%>
aggregation_rules["
<%= rule %>"] = {

 'aggregation': 'worst!1!2',
 'comment': u'All the services associated with the customer',
 'nodes': [<%=
    props
['omd_ip'].zip(props['omd_service']).map { |ip, service|
     
"(u'#{ip}', u'#{service}')"
   
}.join(",\n")
 %>],
 'params': [],
 'title': u'
<%= rule %> Services'
}
<% } -%>

 
I was thinking about using concat but I am a Puppet beginner and I am not really sure how to go about incorporating the erb logic in the concat function.



I don't think you need Concat, and in my view, Concat is one of those things best reserved for when you really need it.  If you did want to use it, though, then you would incorporate ERB by using the template() function to define the contents of one or more fragments.  The beauty of Concat is that you can define different pieces of the target file in different places; this allows a cooperative approach to defining the content that does not depend on all the pieces being known or even numbered in advance.

Note also, however, that if you simply have a fixed number of known pieces from which you want to construct your overall content, then you can either physically combine them into one template, or you can make separate templates and list each one among the arguments to the same template() call.  This is better than Concat if it is applicable.


John

Sergiu Cornea

unread,
Oct 9, 2015, 11:13:00 AM10/9/15
to Puppet Users
Hi John,

Thank you, Thank you very much!!! 

You are a life saviour! 

The example you have provided it is exactly what I need it!

The only thing I wasn't sure it was the file,because I was using define as it was called more than once, however, passing a variable to a class does the same thing.

Thank you once again!

Kind regards,
Sergiu
Reply all
Reply to author
Forward
0 new messages