Calling puppet defined resource with multiple parameters, multiple times

萝らか妹 提交于 2019-12-04 11:33:25

问题


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?


回答1:


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:


来源:https://stackoverflow.com/questions/19024134/calling-puppet-defined-resource-with-multiple-parameters-multiple-times

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!