多次调用具有多个参数的puppet定义的资源

时间:2013-09-26 09:18:00

标签: 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)

但这似乎不起作用。我很相信如果我的资源只使用一个参数并且我只有一个平坦的值数组,这可以工作,但是我可以用多个命名参数做同样的事情吗?

1 个答案:

答案 0 :(得分:7)

这可能适用于您的情况。而不是在变量中定义数组,而是在调用define类型时使它们成为参数。

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";
}

我没有对整个事情进行过测试,我测试过的是这个define,而且它有效:

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

结果:

Mything[k1]/Notify[Yes is 3 not Whatever]/message:
Mything[k2]/Notify[Noo is 17 not Text]/message: