7

我有一个简单的 puppet 定义资源,如下所示:

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()

理想情况下,我会将所有参数存储在某种数组中,然后调用mything($array),有点像这样:

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

mything($array)

但这似乎不起作用。我很确定如果我的资源只接受一个参数并且我只有一个平面数组值,这会起作用,但是我可以用多个命名参数做同样的事情吗?

4

1 回答 1

7

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:
于 2013-09-26T16:51:23.623 回答