Ask Your Question
4

How to iterate over an array and create a resource for each

asked 2013-04-18 16:15:59 -0500

puppetnista gravatar image

updated 2013-04-19 10:35:58 -0500

binford2k gravatar image

I have a fact that returns a list of system drives in JSON format. It returns something like:

{ "system_drives": ["/dev/mapper/vg_l3lv_root","/dev/sda1","/dev/sda2"] }

Then I set a variable to the parsed form of that fact with

$drives = parsejson($::system_drives)

I want to iterate over each element and assign it to a variable to use within my resource definition - in this case a custom resource called "command". The 'title' is not what's being set but the -p <drive_name> portion of parameters parameter.

I want to create a for each drive and have the parameters look like: parameters ... (more)

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2013-04-18 16:56:35 -0500

binford2k gravatar image

updated 2013-09-24 08:42:10 -0500

llowder gravatar image

This is precisely the use case defined types were created for. You'd simply define a type within your module like

define mymodule::drivemap($parameters = '-w 20% -c 10% -p') {
    nrpebasic::command { "checkdisk ${name}":
       ensure     => 'present',
       command    => 'check_disk',
       parameters => "${parameters} ${name}",
    }
}

Then you could declare an instance of this type for each element in your array with something like:

mymodule::drivemap { $drives:
    parameters => 'set this if you want to override the parameters defaults',
}

Refer to documentation for defined types at http://docs.puppetlabs.com/learning/definedtypes.html

Also see the language guide for the array-as-title pattern: http://docs.puppetlabs.com/guides/language_guide.html#arrays

edit flag offensive delete link more

Comments

It looks like the -p in $parameters should actually come before ${name} in the nagios check since it is inherently linked to the device path, according to http://nagiosplugins.org/man/check_disk

Hunner gravatar imageHunner ( 2013-04-18 17:11:58 -0500 )edit

Thanks very much for the replies. I nearly have this working now however parsejson() is causing problems:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax ...(more)

puppetnista gravatar imagepuppetnista ( 2013-04-19 11:24:35 -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

Stats

Asked: 2013-04-18 16:15:59 -0500

Seen: 11,928 times

Last updated: Sep 24 '13