puppet,在使用数组实例化资源类型时获取$name

puppet,在使用数组实例化资源类型时获取$name,puppet,Puppet,使用puppet,我需要创建三个文件,其中包含以下内容: /tmp/f1.txt: hello /tmp/f1.txt /tmp/f2.txt: hello /tmp/f2.txt /tmp/f3.txt: hello /tmp/f3.txt 我尝试如下: $path="/tmp/" $my_files = ["$path/f1.txt", "$path/f2.txt", "$path/f3.txt"] file { $my_files: ensure => file, cont

使用puppet,我需要创建三个文件,其中包含以下内容:

/tmp/f1.txt: hello /tmp/f1.txt
/tmp/f2.txt: hello /tmp/f2.txt
/tmp/f3.txt: hello /tmp/f3.txt
我尝试如下:

$path="/tmp/"
$my_files = ["$path/f1.txt", "$path/f2.txt", "$path/f3.txt"]
file { $my_files:
  ensure => file,
  content => "hello $name\n",
}  
define file_with_content {
  file { $name:
    ensure => file,
    content => "hello $name\n",
  }  
}
$path="/tmp/"
$my_files = ["$path/f1.txt", "$path/f2.txt", "$path/f3.txt"]
file_with_content { $my_files: }
但是,这不起作用,因为$name未定义

是否有一个变量可以为每次“迭代”实例化,并且我可以使用它

ps:我知道我可以创建一个新的资源类型,如下所示:

$path="/tmp/"
$my_files = ["$path/f1.txt", "$path/f2.txt", "$path/f3.txt"]
file { $my_files:
  ensure => file,
  content => "hello $name\n",
}  
define file_with_content {
  file { $name:
    ensure => file,
    content => "hello $name\n",
  }  
}
$path="/tmp/"
$my_files = ["$path/f1.txt", "$path/f2.txt", "$path/f3.txt"]
file_with_content { $my_files: }
但这需要创建新的资源类型, 我不能在我的上下文中这样做(这里没有解释)


问题是,如何修改第一个代码使其工作,而不定义新的资源类型,也不执行shell代码?

对于已定义的类型,您只能访问
namevar
。对于Puppet的资源,结果是不可预测的-例如,
文件的
$name
将为您提供
main
,或当前的
阶段
。此外,您不能向Puppet的资源传递/利用额外的参数,因为它们已经有了自己的参数集


标准的解决方案是将
文件
声明封装在定义的类型中,就像您的第一个声明一样。也许您可以解释为什么不能使用它,这样就可以设计其他解决方案了?

我不能在这里使用define,因为我已经在define中,而且我们似乎不能使用嵌套的define。嵌套的类/定义通常是个坏主意:。它们可以放在外部而不需要太多麻烦,然后在另一个类中引用。什么阻止了你?你可以在一个类中有一个define或class。但是不能在define中包含define。这是正确的吗?