简体   繁体   English

在 puppet 中使用模块 hiera 时遇到一些问题

[英]Having some trouble using module hiera in puppet

I am having some trouble using module hiera data.我在使用模块 hiera 数据时遇到了一些问题。

module: /etc/puppetlabs/code/environments/production/modules/usehiera模块:/etc/puppetlabs/code/environments/production/modules/usehiera

tree structure:树结构:

usehiera
usehiera/hiera.yaml
usehiera/data
usehiera/data/common.yaml
usehiera/manifests
usehiera/manifests/init.pp

hiera.yaml: hiera.yaml:

---
version: 5
defaults:  
  datadir: data
  data_hash: yaml_data
hierarchy:
  - name: 'common'
  - path: 'common.yaml'

data/common.yaml:数据/common.yaml:

---
usehiera::apples: 'this is some data'

manifests/init.pp:清单/init.pp:

class usehiera{
    file{'/tmp/hiera_lookup.txt':
        ensure => present,
        #content => hiera('oranges') #this works with global hiera
        content => $apples
    }
}

As you can see I seem to have the global hiera working with "hiera('oranges')" when I run this module on my node.正如您所看到的,当我在我的节点上运行这个模块时,我似乎让全局 hiera 与“hiera('oranges')”一起工作。 When I try to use the module hiera data the puppet run finishes successfully but hiera_lookup.txt is just empty.当我尝试使用模块 hiera 数据时,木偶运行成功完成,但 hiera_lookup.txt 只是空的。

Steps I have taken to troubleshoot:我已采取的故障排除步骤:

  1. restart puppetserver after hiera changes hiera 更改后重新启动 puppetserver
  2. try using $usehira::apples尝试使用 $usehira::apples
  3. try using hiera('apples')尝试使用 hiera('apples')
  4. moving my hiera.yaml inside data/在 data/ 中移动我的 hiera.yaml
  5. using lookup with --explain doesnt really give me anything useful, just says lookup() not found使用带有 --explain 的查找并没有真正给我任何有用的东西,只是说没有找到 lookup()

Can anyone help me out?谁能帮我吗? I have been stuck with this for a decent amount of time and not sure what the issue could be.我已经被这个问题困扰了很长时间,但不确定可能是什么问题。

As @MattSchuchard pointed out in comments, your hiera.yaml is inappropriately formed.正如@MattSchuchard 在评论中指出的那样,您的hiera.yaml不正确。The documentation contains examples.该文档包含示例。

But a bigger issue seems to be incorrect expectations.但更大的问题似乎是不正确的期望。 Evidently, you assume that ordinary class variable $usehiera::apples will automatically take the value associated with the corresponding key in the module-level hiera data, but that simply is not the case.显然,您假设普通类变量$usehiera::apples将自动采用与模块级 hiera 数据中相应键关联的值,但事实并非如此。 Hiera data -- whether global, environment-level, or module-level -- is automatically bound to class parameters , but not to other class variables. Hiera 数据——无论是全局、环境级别还是模块级别——自动绑定到类参数,但不会绑定到其他类变量。

You can set ordinary class variables from hiera data via explicit lookup:您可以通过显式查找从 hiera 数据设置普通类变量:

# the hiera() function is deprecated; use lookup():
$apples = lookup('usehiera::apples')

Alternatively, you can make $apples a class parameter:或者,您可以将$apples类参数:

class usehiera(String $apples) {
  file{'/tmp/hiera_lookup.txt':
    ensure  => 'present',
    content => $apples,
  }
}

Note well that if you make it a parameter then its value can also be customized via a resource-like class declaration, which takes precedence over your Hiera data.请注意,如果您将其设置为参数,那么它的值也可以通过类资源类声明进行自定义,该类声明优先于您的 Hiera 数据。

Note also that the difference between global, per-environment, and module-specific Hiera data is just scope and precedence, not functionality.另请注意,全局、每个环境和特定于模块的 Hiera 数据之间的区别只是范围和优先级,而不是功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM