Ask Your Question
0

How to itterate over a hash without future parser...

asked 2015-02-07 03:56:03 -0500

Krist van Besien gravatar image

updated 2015-02-07 04:09:44 -0500

I have this hash:

$networks = {
    dev1 => {
      bsl1       => {
        management => {
          cidr => '10.20.8.0/24',
          vlan => 201
        }
        ,
        storage    => {
          cidr => '10.30.8.0/24',
          vlan => 301
        }
        ,
        tenant     => {
          cidr => '10.40.8.0/24',
          vlan => 401
        }
      }
      ,
      zrh1       => {
        management => {
          cidr => '10.21.8.0/24',
          vlan => 201
        }
        ,
        storage    => {
          cidr => '10.31.8.0/24',
          vlan => 301
        }
        ,
        tenant     => {
          cidr => '10.41.8.0/24',
          vlan => 401
        }
      }
      ,
      qa         => {
      }
      ,
      prod       => {
      }
    }
}

(Not all values are present yet, hence some empty hashes...

The idea behing this hash is to describe the different networks we use for different silos and datacenters...

The problem I need to solve now is for a host to "discover" where it is, and it can do this by checking it's primary IP against a list of networks. So what I want to do is create an array, that contains all the CIDR values for "management" networks.

This appears to be exactly the case against for which functions like "each" are meant.

$management_networks = $networks.each |$silo , $value| {
   $value.each |$dc, $value| {
       $value[managment][cidr]
   }
}

I haven't tested if this works however, and maybe map or reduce is a better function. The reason is I don't have future parser enabled, and this is required for the .each function (and similar functions). We can't yet enable future parser, as we have to much code already that needs to be looked at first.

So is there another way to do this?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-02-07 12:37:17 -0500

GregLarkin gravatar image

The inline_template function has been around for a while in several versions of Puppet, and you don't have to enable the future parser to use it, so it might be what you're looking for. You can embed some Ruby code in the function to create a new data structure with your management networks in it.

Once you do that, you can use another inline_template call to determine what network your host belongs to. However, now that you need to use inline_template multiple times, I might consider putting all of the Ruby code into a custom parser function instead. That will result in a cleaner look to your Puppet DSL.

Your DSL would look something like:

$management_network = discover_network(${::ipaddress}, $networks)

Implementation of the discover_network function is left as an exercise for the reader, but I hope that gets you started down the right path.

edit flag offensive delete link more

Comments

That's actually what I did. Was a good opportunity to learn Ruby anyway.

Krist van Besien gravatar imageKrist van Besien ( 2015-02-08 04:25:38 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-02-07 03:56:03 -0500

Seen: 584 times

Last updated: Feb 07 '15