[英]Installing a group of files with puppet and hiera
I have a puppet config that correctly installs a file. 我有一个可以正确安装文件的人偶配置。 I want it to install several files. 我希望它安装几个文件。 The config looks roughly like this (in the part that is relevant here): 配置大致如下所示(在此处相关的部分中):
$stuff = hiera('stuff')
$site_filename = $stuff['site_file']
file { "/path/to/my/file/called/$site_filename":
ensure => present,
owner => 'the-owner',
group => 'the-group',
mode => 644,
source => "puppet:///modules/this-module/$site_filename",
require => [Package['something'],
User['someone']]
}
file { "/path/to/my/symlink/called/$site_filename":
ensure => 'link',
target => "/path/to/my/file/called/$site_filename",
require => Package['something'],
}
Works great, the right file is installed on the right host. 效果很好,在正确的主机上安装了正确的文件。 But I'd now like to install a variable number of (very similar) files, the number being different on each host. 但是我现在想安装可变数量的(非常相似)文件,每个主机上的数量不同。
My hiera files currently look like this: 我的hiera文件当前如下所示:
stuff:
site_file: "hey-i-am-the-site-file-on-host-awesomeness"
In principle, I want to say something like this: 原则上,我想这样说:
stuff:
site_file: ["hey-i-am-the-site-file-on-host-awesomeness",
"i-am-also-a-site-file-for-awesomeness",
"do-not-forget-me-too",
"someday-you-will-want-me-as-well"]
And here I am hitting the limits of my puppet and hiera knowledge. 在这里,我达到了我的木偶和希拉知识的极限。 I understand that when I think I should iterate in puppet, I'm probably wrong, but I'm a bit confused how to do this. 我知道当我认为应该在人偶中进行迭代时,我可能错了,但是我对如何执行此操作感到有些困惑。
Any pointers on how to do it or what to read about to learn? 关于如何做或要学什么的任何指示?
Puppet 4 and has some iteration functions that would be applicable here, and these are available also in recent Puppet 3 with the future parser enabled. Puppet 4并具有一些适用于此的迭代功能,并且在启用了将来的解析器的最新Puppet 3中也可用。 They are not available in Puppet 3 without the future parser, however, so you need a different solution. 如果没有将来的解析器,它们将无法在Puppet 3中使用,因此您需要其他解决方案。
The classic way to approach problems such as this is to rely on the fact that a resource declaration in which the title is an array (literal or array-valued variable) declares a separate resource for each element of the array. 解决此类问题的经典方法是依靠以下事实:标题为数组的资源声明(字面量或数组值变量)为数组的每个元素声明一个单独的资源。 This is often combined with a defined type as the resource to directly declare. 通常将它与定义的类型结合起来作为直接声明的资源。 This combination is roughly equivalent to a foreach loop over the array elements, with the defined-type body as its body. 这种组合大致等效于以定义类型的主体为其主体的数组元素上的foreach循环。 Example: 例:
define mymodule::sitefile() {
file { "/path/to/my/file/called/$title":
ensure => present,
owner => 'the-owner',
group => 'the-group',
mode => 644,
source => "puppet:///modules/mymodule/$title",
require => [Package['something'], User['someone']]
}
file { "/path/to/my/symlink/called/$title":
ensure => 'link',
target => "/path/to/my/file/called/$title",
require => Package['something']
}
}
# ...
$stuff = hiera('stuff')
mymodule::sitefile { $stuff['site_file']: }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.