
假設有以下 Puppet 代碼:
define apache::base($pkgver = '2.4.10') {
$apache_ver = $pkgver
...
}
define apache::vhost($instance) {
...
$apache_ver = getvar(......)
}
apache::base{ "static-files":}
apache::base{ "dynamic": pkgver => '2.4.8' }
apache::vhost{ "static.example.com": instance => "static-files"}
中的程式碼如何apache::vhost
引用對應的$pkgver
(參數)或(變數) ?$apache_ver
apache::base
我們的 stdlib 太舊了(我們的 Puppet 仍然是 2.7.x)並且沒有getparam()
.getvar()
應該能夠做到──但是如何做到呢?在這種情況下變數的全名是什麼?
我嘗試過getvar("apache::base[$instance]::apache_ver")
但getvar("apache::base::$instance::apache_ver")
沒有成功—— getvar 返回一個空字串...正確的方法是什麼?
答案1
你不知道。
要獲得這樣的值,您必須參考實例您定義的類型,例如
Apache::Vhost['main-site']::server_alias
但這樣的事情並沒有實施。
您需要重構您的模型。在您的模組中,apache::vhost
不能獨立於apache::base
.相反,您需要傳遞一個資源哈希,apache::base
以便它可以聲明虛擬主機本身。
define apache::base($pkgver = '2.4.10', $vhosts = {}) {
$apache_ver = $pkgver
...
create_resources('apache::vhost', $vhosts, { instance => $title })
}
並像這樣使用它
apache::base{ "static-files":
vhosts => {
"static.example.com" => {
# attributes for Apache::Vhost["static.example.com"] go here
},
}
}