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

时间:2013-10-10 13:56:10

标签: 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,
  content => "hello $name\n",
}  

然而这不起作用,因为$ name未定义。

是否有一个变量可以为每个'迭代'实例化,我可以使用?

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

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代码?

1 个答案:

答案 0 :(得分:2)

您只能访问定义类型的namevar。对于Puppet的资源,结果是不可预测的 - 例如,$name的{​​{1}}将为您提供File或当前的main。此外,您不能将额外的参数传递给Puppet的资源,因为它们已经有了自己的一组参数。

标准解决方案是将stage声明包装在已定义的类型like here中,就像您的第一个一样。也许你可以解释为什么不能使用它,所以可以设计一些其他的解决方案?