在 Puppet 中,如何访问已定义类型中的变量/属性? [英] In Puppet, how can I access a variable/attribute inside a defined type?
问题描述
我想在已定义类型的实例中引用变量.例如,我可以做什么来引用 b
中 foo a
的 $x
和 $y
?
I would like to reference variables inside instances of defined types. For example, what can I do to reference $x
and $y
of foo a
in bar b
?
define foo($x, $y) {
}
define bar($foo) {
notify { "${::$foo::x}": } # <- how to make this reference work?
}
foo { 'a':
x => 'oh bar may you reference me',
y => 'please'
}
bar { 'b':
foo => Foo['a'],
require => Foo['a']
}
我希望它起作用的原因是一个 foo 实例可能包含许多我不想重复给每个可能需要它们的资源的值.与其一遍又一遍地传递这些值,从而重复我自己,我宁愿传递对它们的容器的引用.
The reason why I would like this to work is that a foo instance may contain many values that I wouldn't like to repeat to each and every resource that might need them. Instead of passing those values again and again, thus repeating myself, I'd rather pass a reference to their container.
我一直在寻找并尝试了很多东西,但似乎无法在任何地方找到这个问题的答案.我知道可以修改属性、引用资源和读取类属性,但是否可以读取资源/定义类型的属性?如果不是,那么最好的解决方法是什么?
I've been looking all over and tried a bunch of things, but can't seem to find an answer to this question anywhere. I know it is possible to amend attributes, reference resources and read class attributes, but is it possible to read attributes of a resource/defined type? If it isn't what is then the best possible work around?
推荐答案
我实际上刚刚发现 Puppetlab 的 stdlib 模块 包含一个 getparam 函数 可以用来解决这个问题.
I've actually just found out that Puppetlab's stdlib module includes a getparam function that can be used to solve this problem.
所以这里终于解决了我自己的问题:
So here is finally the solution to my own question:
define foo($x, $y) {
}
define bar($foo) {
notify { getparam(Foo[$foo], 'x'): }
notify { getparam(Foo[$foo], 'y'): }
}
foo { 'a':
x => 'oh bar may you reference me',
y => 'please'
}
bar { 'b':
foo => 'a'
}
请注意 require =>
似乎不需要.Bar['b']
定义中的 Foo['a']
Please note that the require => Foo['a']
in the definition of Bar['b']
does not appear to be needed.
这篇关于在 Puppet 中,如何访问已定义类型中的变量/属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!