using a template, how do you create multiple files whose names come from an array?

241 views
Skip to first unread message

senorsmile

unread,
Jul 24, 2014, 8:08:09 PM7/24/14
to puppet...@googlegroups.com
For example, I have an array like this:

clusters => [
 
'cluster1',
 
'cluster2',
 
'cluster3',
 
'cluster4',
]



I then have a cluster.init.erb that looks soemthing like this:

start on runlevel
[2345]
stop on runlevel
[!2345]

expect fork
respawn

<% @clusters.each do | cluster | -%>
env PIDFILE
="/var/run/${cluster}.pid"

exec /usr/sbin/program_name --pid-file=$PIDFILE
<% end -%>


That is all theoretical at this point since it doesn't work.
What I need it to do is dynamically generate a certain number of files, equal to the number of files in the array "clusters", whose names I will not know beforehand, and changes from host to host. 





Cristian Falcas

unread,
Jul 25, 2014, 4:14:17 AM7/25/14
to puppet...@googlegroups.com
Feed the array to a define where you call the template.


--
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/8959bc99-9944-4676-ae0f-3c834e74cc37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Yanis Guenane

unread,
Jul 25, 2014, 9:56:57 AM7/25/14
to puppet...@googlegroups.com
You could create a define that would be an intermediate layer. Code
would look like the following :

Manifest
--------

$clusters = ['cluster1', 'cluster2', 'clustern']

myclass::mydefine {$clusters : }


Define
------

class myclass::mydefine {

$path = "/tmp/${name}"

file { $path :
ensure => present,
content => template('myclass/foo.erb'),
}

}

Template
--------

Find my value <%= @name %>


Hope this helps,

--
Yanis Guenane

senorsmile

unread,
Jul 25, 2014, 1:27:21 PM7/25/14
to puppet...@googlegroups.com
So, I have this partially working.  

in /etc/puppet/manifests/nodes.pp I have (all entries truncated and scrubbed): 

node node_example {
 
class { 'ganglia::install::gmetad':

    clusters        
=> [
     
'cluster1',
     
'cluster2',
     
'cluster3',
     
'cluster4',
   
]
}


and under /etc/puppet/modules/ganglia/install/gmetad.pp

class ganglia::install::gmetad () {


  define generate_gmond_init
(
    $init_template
= 'ganglia/gmond_init.erb',
    $conf_template
= 'ganglia/gmond_master_conf.erb',
 
) {
    file
{ "/etc/init/gmond_${name}.conf":
      content
=> template("$init_template"),
   
}
    file
{ "/etc/ganglia/gmond_${name}.conf":
      content
=> template("$conf_template"),
   
}
 
}


  generate_gmond_init
{ $clusters: }



}


Now, in each template I can call 
<%= name %>
in order to call the name var as it passes through each array.  



I need to make this a little more complicated by adding more than a single bit of information; i.e. I need to create a hash of clusters, each with their own data, something like this: 

    clusters        => {
     
'cluster1' => { port => 8655 },
     
'cluster2' => { port => 8656 },
     
'cluster3' => { port => 8657 },
     
'cluster4' => { port => 8658 },
   
}

so that I can access more than just the cluster name in each template.  

senorsmile

unread,
Jul 25, 2014, 1:56:06 PM7/25/14
to puppet...@googlegroups.com
I have solved this using create_resources 
http://docs.puppetlabs.com/references/latest/function.html#createresources

which converts the hash into arrays that a define can digest.  

Cristian Falcas

unread,
Jul 25, 2014, 11:40:27 PM7/25/14
to puppet...@googlegroups.com
I think you can solve like this also:

<% @clusters.each do |key,value| %>
cluster name is <%=key %> and cluster port is <%= value %>
<% end -%>




--
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.

senorsmile

unread,
Jul 25, 2014, 11:42:47 PM7/25/14
to puppet...@googlegroups.com
Ah, I didn't know you could do that.  Reading through the documentation can lead to one's mind melting.  

This may be preferable as create_resources messes up my resource ordering.  


Thanks!

Cristian Falcas

unread,
Jul 25, 2014, 11:47:19 PM7/25/14
to puppet...@googlegroups.com
Just be careful how you call the variables: puppet var is <%= @name%>, local var will be <%= name %>


Reply all
Reply to author
Forward
0 new messages