Puppet working with nested hashes and arrays

puppet

I want to checkout several SVN projects and I need to be able to choose the directory names for each branch or trunk.

Now I want to check out the SVN project called svn_name in the folder service_dir and inside that folder I want to create a trunk folder and a branches folder with all branches.

—SVN folder

——–branches

————— branch1_name

————— branch2_name

——- trunk_name

I have the following structure of my hash

$hash = { 
         'svn_name' => { 
             service_dir  => 'directory_name',
                   branch => [ { branch => '0.1', branch_dir => 'branch1_name'}, 
                               { branch => '0.2', branch_dir => 'branch2_name'} ],
                    trunk => {  service_dir => 'trunk_name'},
        }
}

Now I use create_resources with the hash and my defined type to create the necessary folders and checkout the working copies.

The problem is that I'm unable to iterate through my branch array, I don't know how to access the values inside the hash.

define test (
  $service_dir,
  $branch,
  $trunk
){
  file { "/xxx/${service_dir}/branches/${branch[branch_dir]}":
    ensure => present
  }
}

How do I access the variables? Or is there a much easier way to accomplish this? I don't want to use vcsrepo or similar modules because I'm still new to Puppet and need to practice.

Edit1: I probably don't need the hash for the trunk, right? trunk => 'trunk_name' should be enough

Best Answer

To help you achieving your goal faster I'd highly recommend using the vcsrepo type provided by PuppetLabs on github or puppet forge. This wraps most common VCS tools, including subversion. Just to prevent you from reinventing the wheel.

About your puppet syntax question, I've adapted the example from the manual slightly to fit your problem:

$foo = "key"

$myhash = [ { key       => "some value 0",
              other_key => "some other value 0" },
            { key       => "some value 1",
              other_key => "some other value 1" } ]
notice( $myhash[0][$foo] )
notice( $myhash[1][$foo] )

Testing it:

% puppet apply foo.pp
Notice: Scope(Class[main]): some value 0
Notice: Scope(Class[main]): some value 1
Notice: Finished catalog run in 0.08 seconds
Related Topic