【问题标题】:In puppet, how to retrieve variables inside a class scope from a different parameterized class在 puppet 中,如何从不同的参数化类中检索类范围内的变量
【发布时间】: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 行。
  • 让我们知道您喜欢哪条路线,我们可以提供进一步的帮助。

标签: puppet puppet-3


【解决方案1】:

根据 Puppet 文档 (https://docs.puppet.com/puppet/3.5/lang_classes.html#include-like-behavior),您不能使用带有强制参数的类包含声明。在您的情况下,您可以做的就是不打扰包含,因为您已经通过在节点定义中声明它来处理它。此外,因为两者都在您的节点定义中声明,所以您也希望在那里订购它们。

作为旁注,不提供默认值是一种不好的做法,就像跨模块使用变量一样。

【讨论】:

    猜你喜欢
    • 2014-05-04
    • 2021-07-17
    • 1970-01-01
    • 2015-06-24
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 2021-07-12
    • 2021-02-08
    相关资源
    最近更新 更多