How to iterate on a puppet? Or how to avoid it?

I have a global string variable which is actually an array of names:

"mongo1, mongo2, mongo3"

What I am doing here is splitting them into an array using "," as a separator, and then feeding that array into the definition to create all the instances I want.

The problem is that each instance has a different port. I created a new function stdlib to get the index of the name in the array and feed it into the port parameter.

This seems bad and I don't like changing stdlib.

So, I'm wondering how can I do this using something like an nx2 array?

"mongo1, port1; mongo2, port2; mongo3, port3"

or two arrays

"mongo1, mongo2, mongo3" and "port1, port2, port3"

class site::mongomodule {
  class { 'mongodb':
    package_ensure => '2.4.12',
    logdir         => '/var/log/mongodb/'
  }

  define mongoconf () {
    $index = array_index($::site::mongomodule::mongoReplSetName_array, $name)

    mongodb::mongod { "mongod_${name}":
      mongod_instance => $name,
      mongod_port     => 27017 + $index,
      mongod_replSet  => 'Shard1',
      mongod_shardsvr => 'true',
    }
  }

  $mongoReplSetName_array = split(hiera('site::mongomodule::instances', undef), ',')

  mongoconf { $mongoReplSetName_array: }
}

      

the module I am using is the following:

https://github.com/echocat/puppet-mongodb


using puppet 3.8.0

+3


source to share


2 answers


Hiera can give you a hash when looking up a key, so you can have something like this in hiera:

mongoinstances:
  mongo1:
    port: 1000
  mongo2:
    port: 1234

      

Then you look at the key in hiera to get the hash and pass it to a function create_resources

that will instantiate one resource per hash entry.



$mongoinstances = hiera('mongoinstances')
create_resources('mongoconf', $mongoinstances)

      

For this you will need to change mongoconf

by adding a parameter $port

. Anytime you want to pass an additional value from hiera, just add it as a parameter to your specific type.

+4


source


If you are using puppet> = 4.0 use puppet hashes with every function.

Define a hash , for example:

$my_hash = { mongo1 => port1,
             mongo2 => port2, } 

      



Then use each like:

$my_hash.each |$key, $val| { some code }.

      

Read more about iteration in puppetry here .

+1


source







All Articles