【发布时间】:2017-06-02 23:07:53
【问题描述】:
在一个类的范围内,我需要能够访问另一个类的变量。变量作为参数传递,例如
class parameterized_class (
$param1,
) {
...
}
和
class other_class () {
include parameterized_class
Class['parameterized_class'] -> Class['other_class']
$local_var = $parameterized_class::param1
}
举个例子:
node default {
class { 'parameterized_class':
param1 => 'somevalue',
}
class { 'other_class': }
}
上面的例子不起作用,因为我得到一个大致如下的错误:
必须将 param1 传递给 类 [Parameterized_class] 在 /path/to/modules/parameterized_class/manifests/init.pp:1 在节点本地主机上
显然,include 试图在不传递任何参数的情况下声明 parameterized_class。但是从文档中,我可以看到 include 允许之前已经声明了一个类,并且由于我将 parameterized_class 声明作为 other_class 的依赖项,所以我不明白我怎么能得到这个错误。
我正在使用 Puppet 3.4.3,Ubuntu 14.04 Trusty 上可用的版本
我应该如何从other_class 的范围内检索parameterized_class 中的$param1 的值?是不是不能在参数化类上使用include?
【问题讨论】:
-
首先,升级到 Puppet 的非过时版本:docs.puppet.com/puppet/4.10/…。其次,您可以将
include与参数化类一起使用,但这并不能帮助您在这里实现您想要的。第三,在other_class定义中包含Class['parameterized_class'] -> Class['other_class']以及您尝试做事的方式可能最终会导致问题。鉴于此以及您正在尝试做的事情,解决问题的最简单方法是 hiera 或删除include parameterized_class行。 -
让我们知道您喜欢哪条路线,我们可以提供进一步的帮助。