iterating over an array of hashs

1,359 views
Skip to first unread message

spokra

unread,
Feb 14, 2012, 11:40:18 AM2/14/12
to Puppet Users
Since there are no looping constructs in puppet other that passing an
array to a defined resource.
I'm passing an array of hashs to a defined resource and I'm getting a
string that is the contests of the hash. is there away in puppet to
cast a variable to the correct data type? Or is there a better way
of accomplishing this?

My code is at the following github gist

https://gist.github.com/1827882

Gary Larizza

unread,
Feb 14, 2012, 11:53:00 AM2/14/12
to puppet...@googlegroups.com
Steven,

You can use Hashes all the way.  See --> https://github.com/glarizza/puppet-datademo/blob/master/hieradb/puppetmaster.puppetlabs.vm.yaml  for my Hiera YAML file and declare with:

$war_installs = hiera_hash('war_installs')
create_resources('tomcat::war', $war_installs)


Does this help?



--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.




--

Gary Larizza
Professional Services Engineer
Puppet Labs

Steven Pokrandt

unread,
Feb 14, 2012, 2:04:26 PM2/14/12
to puppet...@googlegroups.com
here is my code, yaml and error:

what am i missing here? 

define tomcat::wars($environment,$hostname) {
  $instance=$name
  $wars = hiera_hash('wars')
  create_resources('tomcat::war',$wars)

}

class tomcat::war ( $wars = {} ) {
  notice($wars)
}


wars:
  'card-managment':
     app_version    : '1.44'
     app_url        : 'puppet:///card-management/${VERSION}/card-management-${VERSION}.war'
     app_target_dir : 'card-management'
  'perscription-transfer':
     app_version    : '1.18'
     app_url        : 'puppet:///prescription-transfer-webapp/${VERSION}/prescription-transfer-webapp-${VERSION}.war'
     app_target_dir : 'perscription-transfer'
  'pharmacy':
     app_version    : '1.62'
     app_url        : 'puppet:///pharmacy-webapp/${VERSION}/pharmacy-webapp-${VERSION}.war'
     app_target_dir : 'pharmacy-webapp'




could not create resource of unknown type tomcat::war at /etc/puppet/modules/tomcat/manifests/wars.pp:4 on node pokrsx1puppetagent.ghc.org
Steven Pokrandt  (425) 686 WABO
http://steven.pokrandt.me/


Gary Larizza

unread,
Feb 14, 2012, 2:13:56 PM2/14/12
to puppet...@googlegroups.com
On Tue, Feb 14, 2012 at 11:04 AM, Steven Pokrandt <spo...@gmail.com> wrote:
here is my code, yaml and error:

what am i missing here? 

define tomcat::wars($environment,$hostname) {
  $instance=$name
  $wars = hiera_hash('wars')
  create_resources('tomcat::war',$wars)

These two lines should be in your site.pp file and NOT the defined resource type (essentially, they are creating resource FOR the defined resource type for every instance in Hiera).  Also, check your spelling - it should read: create_resources('tomcat::wars',$wars)   (note the plurality of tomcat::wars).

Steven Pokrandt

unread,
Feb 14, 2012, 2:33:57 PM2/14/12
to puppet...@googlegroups.com

We run many tomcat instances on each machine.  and the instance is not know until another hiera call is made.  is it not possible to use create resource in a class? 

Gary Larizza

unread,
Feb 14, 2012, 2:38:42 PM2/14/12
to puppet...@googlegroups.com
Inside a class is fine, but notice that you did it INSIDE the very defined resource type that you're calling out to (i.e. you're trying to declare resources of type yum::wars INSIDE the parameter declaration section OF the yum::wars defined resource type)  :)  

Try putting them in another class (or directly in your site.pp) and you should be good to go!

Steven Pokrandt

unread,
Feb 14, 2012, 3:01:11 PM2/14/12
to puppet...@googlegroups.com
ok now that I have that working...  thank you!!   I have one more question,,

say i have the following strings.

$version="1.2.3"
$url="/path/to/file-${version}"


is there have puppet do the replacement as if it were 
$version="1.2.3"
$string = "/path/to/file-${version}"

Gary Larizza

unread,
Feb 14, 2012, 3:34:46 PM2/14/12
to puppet...@googlegroups.com
In Hiera? Use %{} in place of ${} and give it a shot :)
Reply all
Reply to author
Forward
0 new messages