问题描述
使用模块hiera数据时遇到一些麻烦。
模块:/ etc / puppetlabs / code / environments / production / modules / usehiera
树结构:
usehiera
usehiera/hiera.yaml
usehiera/data
usehiera/data/common.yaml
usehiera/manifests
usehiera/manifests/init.pp
hiera.yaml:
---
version: 5
defaults:
datadir: data
data_hash: yaml_data
hierarchy:
- name: 'common'
- path: 'common.yaml'
data / common.yaml:
---
usehiera::apples: 'this is some data'
清单/init.pp:
class usehiera{
file{'/tmp/hiera_lookup.txt':
ensure => present,#content => hiera('oranges') #this works with global hiera
content => $apples
}
}
如您所见,当我在节点上运行此模块时,我似乎使全局hiera与“ hiera('oranges')”一起使用。当我尝试使用模块hiera数据时,木偶运行成功完成,但是hiera_lookup.txt只是空的。
我已采取的措施进行故障排除:
- 更改等级后重新启动puppetserver
- 尝试使用$ usehira :: apples
- 尝试使用hiera('apples')
- 在数据中移动我的hiera.yaml /
- 通过--explain使用lookup并不能真正给我任何有用的信息,只是说没有找到lookup()
有人可以帮我吗?我已经坚持了很长时间,但不确定是什么问题。
解决方法
正如@MattSchuchard在评论中指出的那样,您的hiera.yaml
的格式不正确。 The documentation包含示例。
但是更大的问题似乎是不正确的期望。显然,您假定普通类变量$usehiera::apples
将自动采用与模块级层次结构数据中的对应键相关联的值,但事实并非如此。层次结构数据-无论是全局,环境级别还是模块级别-都自动绑定到类参数,而不绑定到其他类变量。
您可以通过显式查找从hiera数据中设置普通类变量:
# the hiera() function is deprecated; use lookup():
$apples = lookup('usehiera::apples')
或者,您可以将$apples
设为类参数:
class usehiera(String $apples) {
file{'/tmp/hiera_lookup.txt':
ensure => 'present',content => $apples,}
}
请注意,如果将其设置为参数,则还可以通过类似于资源的类声明来自定义其值,该类声明优先于您的Hiera数据。
还请注意,全局,每个环境和特定于模块的Hiera数据之间的区别只是范围和优先级,而不是功能。