Haitao Jiang
2011-Jun-30 01:16 UTC
[Puppet Users] How to define a hash table and loop over them in a definition?
This may have been asked before, if so, please excuse me and point me to the right direction. What I want to do is to define a hash table with multiple key->value pairs. I would like to pass this variable to a file template and generate a result file with all the mappings listed. Based on what I read, I need to do it in a definition. But does Puppet supports loop over a hash? Any help would be highly appreciated. I am using Puppet 2.6.8 Thanks -- 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.
treydock
2011-Jun-30 01:56 UTC
[Puppet Users] Re: How to define a hash table and loop over them in a definition?
Here''s an example of how I have done this, and also a good way to test the idea... I created test.pp with these contents...you can also specify the $apparray else where, for example in the node definition $apparray = { app1 => { ''path'' => ''/test/path1'', ''command'' => ''cmd1'' }, app2 => { ''path'' => ''/test/path2'', ''command'' => ''cmd2'' }, } file { "/etc/puppet/output": content => template("/etc/puppet/test.erb") } Then create the template file that the hash is used in, test.erb, this is where you loop through your values <% apparray.each do |key,value| -%> Key: <%= key %> Path: <%= value[''path''] %> Command: <%= value[''command''] %> <% end -%> Then to generate the output file you run ... # puppet test.pp notice: /Stage[main]//File[/etc/puppet/output]/content: content changed ''{md5}c473dbf9c2539d14a3042f81ab2edafd'' to ''{md5} 9568aa118a031c5621c65c36bbe34bfe'' notice: Finished catalog run in 0.03 seconds The output file should look something like this Key: app1 Path: /test/path1 Command: cmd1 Key: app2 Path: /test/path2 Command: cmd2 - Trey On Jun 29, 8:16 pm, Haitao Jiang <jianghai...@gmail.com> wrote:> This may have been asked before, if so, please excuse me and point me > to the right direction. > > What I want to do is to define a hash table with multiple key->value > pairs. I would like to pass this variable to a file template and > generate a result file with all the mappings listed. Based on what I > read, I need to do it in a definition. But does Puppet supports loop > over a hash? > > Any help would be highly appreciated. I am using Puppet 2.6.8 > > Thanks-- 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.
vagn scott
2011-Jun-30 02:11 UTC
Re: [Puppet Users] Re: How to define a hash table and loop over them in a definition?
On 06/29/2011 09:56 PM, treydock wrote:> <% apparray.each do |key,value| -%> > > Key:<%= key %> > Path:<%= value[''path''] %> > Command:<%= value[''command''] %> > > <% end -%>inline_template() can be used as a here document. that plus a puppet shebang line makes testing and presenting examples really easy. Put the following in file here-hash.pp, then chmod +x here-hash.pp ./here-hash.pp --vagn ---------------8<---------------------------------------------------------------------------- #! /usr/bin/puppet apply Exec { path => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" } $prog_name = "here-hash.pp" $apparray = { app1 => { ''path'' => ''/test/path1'', ''command'' => ''cmd1'' }, app2 => { ''path'' => ''/test/path2'', ''command'' => ''cmd2'' }, } $result = inline_template(" <% apparray.each do |key,value| -%> Key: <%= key %> Path: <%= value[''path''] %> Command: <%= value[''command''] %> <% end -%> ") node default { notice("--- running: $program_name -------------------------------------") notice($result) notice("--- done: $program_name ----------------------------------------") } -- 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.
Haitao Jiang
2011-Jun-30 17:42 UTC
Re: [Puppet Users] Re: How to define a hash table and loop over them in a definition?
Thanks a lot for both answers! Very helpful. However, I was a little surprised that how slow Puppet was when I run your example. Was it Puppet just slow or was it just my VM is slow? I literally had to wait 1 min to get the result: Thu Jun 30 10:37:56 PDT 2011 notice: Finished catalog run in 0.02 seconds Thu Jun 30 10:38:58 PDT 2011 Did I do anything wrong here? I was running it under Ubuntu 10.04 Thanks again! On Wed, Jun 29, 2011 at 7:11 PM, vagn scott <vagnscott@gmail.com> wrote:> On 06/29/2011 09:56 PM, treydock wrote: >> >> <% apparray.each do |key,value| -%> >> >> Key:<%= key %> >> Path:<%= value[''path''] %> >> Command:<%= value[''command''] %> >> >> <% end -%> > > inline_template() can be used as a here document. > that plus a puppet shebang line makes testing > and presenting examples really easy. > > Put the following in file here-hash.pp, then > > chmod +x here-hash.pp > ./here-hash.pp > > --vagn > > ---------------8<---------------------------------------------------------------------------- > > #! /usr/bin/puppet apply > > Exec { > path => > "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" > } > > $prog_name = "here-hash.pp" > > $apparray = { > app1 => { ''path'' => ''/test/path1'', ''command'' => ''cmd1'' }, > app2 => { ''path'' => ''/test/path2'', ''command'' => ''cmd2'' }, > } > > $result = inline_template(" > > <% apparray.each do |key,value| -%> > > Key: <%= key %> > Path: <%= value[''path''] %> > Command: <%= value[''command''] %> > > <% end -%> > > ") > > > node default { > > notice("--- running: $program_name > -------------------------------------") > > notice($result) > > notice("--- done: $program_name > ----------------------------------------") > } > > -- > 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. > >-- 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.
vagn scott
2011-Jul-01 08:25 UTC
Re: [Puppet Users] Re: How to define a hash table and loop over them in a definition?
Maybe you''ve got network issues? 1 minute sounds like two DNS lookups timing out. -- vagn On 06/30/2011 01:42 PM, Haitao Jiang wrote:> Thanks a lot for both answers! Very helpful. > > However, I was a little surprised that how slow Puppet was when I run > your example. Was it Puppet just slow or was it just my VM is slow? I > literally had to wait 1 min to get the result: > > Thu Jun 30 10:37:56 PDT 2011 > notice: Finished catalog run in 0.02 seconds > Thu Jun 30 10:38:58 PDT 2011 > > Did I do anything wrong here? I was running it under Ubuntu 10.04 > > Thanks again! >-- 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.
jcbollinger
2011-Jul-01 12:58 UTC
[Puppet Users] Re: How to define a hash table and loop over them in a definition?
On Jun 30, 12:42 pm, Haitao Jiang <jianghai...@gmail.com> wrote:> Thanks a lot for both answers! Very helpful. > > However, I was a little surprised that how slow Puppet was when I run > your example. Was it Puppet just slow or was it just my VM is slow? I > literally had to wait 1 min to get the result: > > Thu Jun 30 10:37:56 PDT 2011 > notice: Finished catalog run in 0.02 seconds > Thu Jun 30 10:38:58 PDT 2011 > > Did I do anything wrong here? I was running it under Ubuntu 10.04Puppet reported spending just 20 milliseconds applying the catalog. Your puppetmaster''s log will show how long it spent compiling that catalog. Any other elapsed time is not directly attributable to Puppet, but is not necessarily the result of running in VM, either. It could be a result of multiple VMs contending for processor and/or network resources, of a network configuration problem (e.g. Vagn''s DNS timeout), or of simple network congestion. There are undoubtedly other possibilities as well. John -- 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.
Seemingly Similar Threads
- err: Could not retrieve catalog from remote server: Could not intern from pson: Could not convert from pson: Could not find relationship target "File[]"
- [PATCH] launch: switch from -nographic to -display none
- [PATCH v2 0/2] lib: qemu: Memoize qemu feature detection.
- Hash Interpolation inside double quotes?
- time command