James Cammarata
2010-Jan-22 17:35 UTC
[Puppet Users] Best method for using array variables in classes
Hi, we''re trying to re-use some variables that are currently used for templating a configuration file. We found out these same hosts need to be in our /etc/hosts file as well, so we''d like to use the built-in host type to do this, but since our list of hosts is stored as an array, I''m having some issues. Here''s what we have: $my_servers = ["server1.domain|1.1.1.1","server2.domain|1.1.1.2"] Our template does this (we need both the FQDN and the short name in the config file, for software reasons): <% my_servers.each do |server| %> SERVER = <%= server.split(''|'')[0] %> SERVER = <%= server.split(''|'')[0].split(''.'')[0] %> So, how can we re-use the array of data we have so that we don''t need to recreate things? I was looking at doing something like this: define multihosts($line) { $hostname = split($line, "[|]")[0] $ip = split($line, "[|]")[1] ... host { "$name_$hostname": name => $hostname, ... } } multihosts { foo: line => $my_servers[0] } But puppet does not seem to like indexing variables inside classes, and I can''t find anywhere in the language reference where to do this currently. Also, despite the fact that the split() function is in the function reference, I am getting the following error: err: Could not retrieve catalog: Unknown function split at /var/lib/puppet/modules/... on node mynode So, what am I doing wrong, and what is the best way to proceed? -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
John T. Guthrie
2010-Jan-22 19:17 UTC
Re: [Puppet Users] Best method for using array variables in classes
On 01/22/2010 12:35 PM, James Cammarata wrote:> But puppet does not seem to like indexing variables inside classes, and I > can''t find anywhere in the language reference where to do this currently. > Also, despite the fact that the split() function is in the function > reference, I am getting the following error: > > err: Could not retrieve catalog: Unknown function split at > /var/lib/puppet/modules/... on node mynode > > > So, what am I doing wrong, and what is the best way to proceed? >If I am not mistaken, the split() function is not in 0.24, and did not get added until 0.25. What version of puppet are you running? John Guthrie -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
James Cammarata
2010-Jan-22 19:25 UTC
Re: [Puppet Users] Best method for using array variables in classes
On Fri, 22 Jan 2010 14:17:26 -0500, "John T. Guthrie" <jguthrie@limewire.com> wrote:> On 01/22/2010 12:35 PM, James Cammarata wrote: >> But puppet does not seem to like indexing variables inside classes, andI>> can''t find anywhere in the language reference where to do thiscurrently.>> Also, despite the fact that the split() function is in the function >> reference, I am getting the following error: >> >> err: Could not retrieve catalog: Unknown function split at >> /var/lib/puppet/modules/... on node mynode >> >> >> So, what am I doing wrong, and what is the best way to proceed? >> > If I am not mistaken, the split() function is not in 0.24, and did not > get added until 0.25. What version of puppet are you running? > > John GuthrieAhh ok, we are running 24.8, so that would explain that one. -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
John T. Guthrie
2010-Jan-22 19:58 UTC
Re: [Puppet Users] Best method for using array variables in classes
On 01/22/2010 02:25 PM, James Cammarata wrote:> On Fri, 22 Jan 2010 14:17:26 -0500, "John T. Guthrie" > <jguthrie@limewire.com> wrote: > >> On 01/22/2010 12:35 PM, James Cammarata wrote: >> >>> But puppet does not seem to like indexing variables inside classes, and >>> > I > >>> can''t find anywhere in the language reference where to do this >>> > currently. > >>> Also, despite the fact that the split() function is in the function >>> reference, I am getting the following error: >>> >>> err: Could not retrieve catalog: Unknown function split at >>> /var/lib/puppet/modules/... on node mynode >>> >>> >>> So, what am I doing wrong, and what is the best way to proceed? >>> >>> >> If I am not mistaken, the split() function is not in 0.24, and did not >> get added until 0.25. What version of puppet are you running? >> >> John Guthrie >> > Ahh ok, we are running 24.8, so that would explain that one. >One workaround is to create your own function mysplit() that duplicates the functionality of the 0.25 split() function. I suppose if you wanted to, you could even grab the split.rb file from the latest puppet source code, and use that as a custom function if you are not ready to upgrade your version of puppet yet. John Guthrie -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
James Cammarata
2010-Jan-22 20:22 UTC
Re: [Puppet Users] Best method for using array variables in classes
> One workaround is to create your own function mysplit() that duplicates > the functionality of the 0.25 split() function. I suppose if you wanted > to, you could even grab the split.rb file from the latest puppet source > code, and use that as a custom function if you are not ready to upgrade > your version of puppet yet. > > John GuthrieI think I''ve found a hack to workaround all of this using inline_template to assign my variables. define multihosts($line) { $hostname = inline_template("<%= line.split(''|'')[0] %>") $alias = inline_template("<%= line.split(''|'')[0].split(''.'')[0] %>") $ip = inline_template("<%= line.split(''|'')[1] %>") host{"${name}_${hostname}": name=>$hostname, alias=>$alias, ip=>$ip, ensure=>present} } and then: multihosts { multihosts_1: line => inline_template("<%= my_servers[0] %>") } multihosts { multihosts_2: line => inline_template("<%= my_servers[1] %>") } This seems to work, even if it is ugly as sin :) -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Thomas Bellman
2010-Jan-22 20:41 UTC
Re: [Puppet Users] Best method for using array variables in classes
James Cammarata wrote:> Hi, we''re trying to re-use some variables that are currently used for > templating a configuration file. We found out these same hosts need to be > in our /etc/hosts file as well, so we''d like to use the built-in host type > to do this, but since our list of hosts is stored as an array, I''m having > some issues.Synchronicity! I did exactly that just two days ago.> $my_servers = ["server1.domain|1.1.1.1","server2.domain|1.1.1.2"][...]> define multihosts($line) { > $hostname = split($line, "[|]")[0] > $ip = split($line, "[|]")[1] > ... > host { "$name_$hostname": name => $hostname, ... } > } > > multihosts { foo: line => $my_servers[0] } > > But puppet does not seem to like indexing variables inside classes, and I > can''t find anywhere in the language reference where to do this currently.Indeed, the Puppet DSL doesn''t support indexing yet.> Also, despite the fact that the split() function is in the function > reference, I am getting the following error: > > err: Could not retrieve catalog: Unknown function split at > /var/lib/puppet/modules/... on node mynodeAs has already been mentioned, split() didn''t come until 0.25. And even if you had split(), since you can''t index, you can''t do it anyway. Luckily, there is the regsubst() function, which can be put to perhaps surprising use. Here''s what I did: define hostentry($ipcolumn=2) { $pattern = ''^\s*(\S+)\s+(\S+)\s+(\S+)\s*$'' $hostname = regsubst($name, $pattern, ''\1'') $ipno = regsubst($name, $pattern, "\\$ipcolumn") host { $hostname: ip => $ipno; } } In my case, the fields of each entry in the array are separated by whitespace, not by the pipe character, and I have several columns, and I can choose which column to pick for the IP address using the ipcolumn parameter. For your format, you would use ''^([^|]*)[|]([^|]*)'' for $pattern (untested). You can then do: hostentry { $my_servers: ; } and off you go. /Bellman -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.