如何将两个或多个变量传递给 Puppet 中的 Define

How to pass two or more variables to Define in Puppet

我想在定义中传递多个参数。

以下是我的代码。我想在定义中传递两个数组,但我只能传递一个,如下所示。

 class test {   
    $path = [$path1,$path2]
    $filename = [$name1,$name2]
    define testscript { $filename: } // Can able to pass one value. 
 }

 define testscript () {
     file {"/etc/init.d/${title}": //Can able to receive the file name.
           ensure  => file,
           content => template('test/test.conf.erb'), 
 }

从上面的代码中,我可以检索定义资源中的 filename。我还需要 path 来设置模板中的值。我无法在模板中发送/检索第二个参数。

有什么方法可以改进我的代码以在定义资源中传递两个值($path$filename)?

非常感谢任何帮助。

Is there any way to improve my code to pass the two values ( $path and $filename ) inside define resource ?

Puppet 有很好的文档,covers this area 很好。

首先,您需要了解已定义的 type 是一种资源类型,几乎在所有方面都类似于任何 built-in 或扩展类型。如果您定义的类型接受参数,那么您可以像在任何其他资源声明中那样将值绑定到这些参数。例如:

class mymodule::test {   
   mymodule::testscript { $name1: path => $path1 }
   mymodule::testscript { $name2: path => $path2 }
}

define mymodule::testscript ($path) {
  file {"${path}/${title}":
    ensure  => 'file',
    content => template('test/test.conf.erb')
  }
}

此外,因为定义的类型是资源类型,所以您应该放弃 "passing" 值的概念,就好像它们是函数一样。这种心智模式很可能会背叛你。特别是,如果您指定数组或散列作为资源标题,它肯定会给您错误的期望。

特别是,您需要了解在任何资源声明中,如果您将资源标题作为数组提供,则意味着每个数组成员都有一个单独的资源,数组成员作为该资源的标题。在这种情况下,这些资源中的每一个都接收相同的参数值,如声明的 body 中所声明的那样。此外,资源标题始终是字符串。除了一层数组,如上所述,如果您提供任何其他内容作为资源标题,那么它将被转换为字符串。