Calling puppet defined resource with multiple parameters, multiple times

11,230

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:
Share:
11,230
growse
Author by

growse

Updated on June 25, 2022

Comments

  • growse
    growse almost 2 years

    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?

  • growse
    growse over 10 years
    Interesting - didn't know you could do that. I guess my reason for wanting to put them into a variable is if I wanted to pass that structure off to more than one define, but this is certainly neater than what I had originally.