Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling puppet defined resource with multiple parameters, multiple times

Tags:

puppet

I've got a simple puppet defined resource that looks like this:

define mything($number, $device, $otherthing) {
    file{"/place/${number}":
        ensure => directory
    }
    mount { "/place/${number}":
        device => $device,
        ensure => mounted,
        require => File["/place/${number}"]
    }
    file {"/place/${number}/${otherthing}":
        ensure => directory,
        require => Mount['/place/${number}']
    }
}

I need to call this resource a number of times with different parameters, but can't figure out how to do this without explicitly calling mything() repeatedly.

Ideally, I'd have all the parameters for the stored in some sort of array, and then just call mything($array), a bit like this:

$array = [
    {number => 3, something => 'yes', otherthing => 'whatever'},
    {number => 17, something => 'ooo', otherthing => 'text'},
    {number => 4, something => 'no', otherthing => 'random'},
]

mything($array)

But this doesn't appear to work. I'm fairly sure this would work if my resource only took a single parameter and I just had a flat array of values, but can I do the same thing with multiple named parameters?

like image 841
growse Avatar asked Sep 26 '13 09:09

growse


1 Answers

This may work for your case. Instead of defining the array in a variable, make them parameters when calling the define type.

define mything($number, $device, $otherthing) {
    file{"/place/${number}":
        ensure => directory
    }
    mount { "/place/${number}":
        device => $device,
        ensure => mounted,
        require => File["/place/${number}"]
    }
    file {"/place/${number}/${otherthing}":
        ensure => directory,
        require => Mount['/place/${number}']
    }
}

mything {
    "k1" : number => "3", device => "Yes", otherthing => "Whatever";
    "k2" : number => "17", device => "Noo", otherthing => "Text";
    "k3" : number => "5", device => "Oui", otherthing => "ZIP";
}

I haven't tested the entire thing, what I have tested is this define instead and it works:

define mything($number, $device, $otherthing){
  notify{"$device is $number not $otherthing":}
}

Results :

Mything[k1]/Notify[Yes is 3 not Whatever]/message:
Mything[k2]/Notify[Noo is 17 not Text]/message:
like image 55
iamauser Avatar answered Oct 16 '22 17:10

iamauser