使用多个参数多次调用 puppet 定义的资源 [英] Calling puppet defined resource with multiple parameters, multiple times

查看:36
本文介绍了使用多个参数多次调用 puppet 定义的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 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}']
    }
}

我需要使用不同的参数多次调用此资源,但如果不重复显式调用 mything(),我无法弄清楚如何执行此操作.

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.

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

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?

推荐答案

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

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

我还没有测试整个事情,我测试的是这个 define 并且它有效:

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

结果:

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

这篇关于使用多个参数多次调用 puppet 定义的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆