[英]Installing a group of files with puppet and hiera
我有一個可以正確安裝文件的人偶配置。 我希望它安裝幾個文件。 配置大致如下所示(在此處相關的部分中):
$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'],
}
效果很好,在正確的主機上安裝了正確的文件。 但是我現在想安裝可變數量的(非常相似)文件,每個主機上的數量不同。
我的hiera文件當前如下所示:
stuff:
site_file: "hey-i-am-the-site-file-on-host-awesomeness"
原則上,我想這樣說:
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"]
在這里,我達到了我的木偶和希拉知識的極限。 我知道當我認為應該在人偶中進行迭代時,我可能錯了,但是我對如何執行此操作感到有些困惑。
關於如何做或要學什么的任何指示?
Puppet 4並具有一些適用於此的迭代功能,並且在啟用了將來的解析器的最新Puppet 3中也可用。 如果沒有將來的解析器,它們將無法在Puppet 3中使用,因此您需要其他解決方案。
解決此類問題的經典方法是依靠以下事實:標題為數組的資源聲明(字面量或數組值變量)為數組的每個元素聲明一個單獨的資源。 通常將它與定義的類型結合起來作為直接聲明的資源。 這種組合大致等效於以定義類型的主體為其主體的數組元素上的foreach循環。 例:
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.