Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/53.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何传递两个或多个变量以在Puppet中定义_Puppet_Puppet Enterprise_Librarian Puppet - Fatal编程技术网

如何传递两个或多个变量以在Puppet中定义

如何传递两个或多个变量以在Puppet中定义,puppet,puppet-enterprise,librarian-puppet,Puppet,Puppet Enterprise,Librarian Puppet,我在define中传递了多个参数 下面是我的代码。我想在define中传递两个数组,但只能传递一个,如下所示 class test { $path = [$path1,$path2] $filename = [$name1,$name2] define testscript { $filename: } // Can able to pass one value. } define testscript () { file {"/etc/init

我在define中传递了多个参数

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

 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'), 
 }
从上面的代码中,我可以在define资源中检索
文件名
。我还需要
path
来设置模板中的值。我无法发送/检索模板中的第二个参数

有没有办法改进我的代码,在define资源中传递两个值(
$path
$filename

非常感谢您的帮助

有没有办法改进我的代码,在define资源中传递两个值($path和$filename)

Puppet有很好的文档,这很好

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

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')
  }
}
此外,因为定义的类型是资源类型,所以应该放弃“传递”值的概念,就像它们是函数一样。这种心理模式很可能会背叛你。特别是,如果将数组或哈希指定为资源标题,它肯定会给您错误的预期


特别是,您需要了解,在任何资源声明中,如果将资源标题作为数组,那么这意味着每个数组成员都有一个单独的资源,数组成员作为该资源的标题。在这种情况下,这些资源中的每一个都会收到与声明主体中声明的相同的参数值。此外,资源标题总是字符串。除了一级数组之外,如上所述,如果您将其他任何内容作为资源标题,那么它将转换为字符串。

从代码中,您无法在定义的类型正文中检索文件名,因为您的类包含语法错误。@JohnBollinger:oops,请指出它好吗?因此,我可以在将来修复它。正如Puppet本身告诉您的那样,在声明已定义类型的实例时,不要使用
define
关键字。正如我在回答中所说,您使用的定义类型与任何其他资源类型完全相同。@JohnBollinger:谢谢您给出如此精彩的回答。