基于模式的 Hiera 查找

Zor*_*che 7 puppet hiera

我正在努力思考如何从我在大型 site.pp 文件中所做的事情转换为我可以在 hiera 中使用的结构。通过阅读 puppet 文档,我不清楚如何评估 hiera 数据以及何时适合图片。我最近从 puppet 2.7.x 升级到 3.3.x。这包括 hiera 作为标准包的一部分,所以我想最终考虑使用它,因为它应该使我的设置更易于阅读/理解。

我正在使用多个外部组织来支持系统。这包括配置每个组织独有的系统。在我的 site.pp 的顶部,我有一个如下所示的结构。我使用它根据与 clientcert 事实匹配的正则表达式为每个组织设置事实,这些事实以可靠地识别每个组织的方式配置和发布。

# match organization
case $::clientcert {

  /.*example1.org/ :
      { $snmp_ro_community='...'
        $snmp_location='Example Org 1' 
        ... }
  /.*example2.org/ :
      { $snmp_ro_community='...'
        $snmp_location='Example Org 2' 
        ... }
  /.*example3.org/ :
      { $snmp_ro_community='...'
        $snmp_location='Example Org 3' 
        ... }
  /.*example4.org/ :
      { $snmp_ro_community='...'
        $snmp_location='Example Org 4' 
        ... }
}
Run Code Online (Sandbox Code Playgroud)

我浏览了示例,但在 hiera.yaml 文件中看不到任何方法可以进行任何类型的模式匹配。我怀疑我一定遗漏了一些明显的东西。

我不想为此依赖自定义事实。我更喜欢坚持使用客户端证书,因为我确信这将正确识别组织和系统,并且已经使用强密码术进行了确认。我不想将一个组织的价值观赋予另一个组织。

Tom*_*art 1

您可以为此设置 YAML 文件的层次结构。让我们从以下开始hiera.yaml

---
:hierarchy:
    - "host/%{fqdn}"
    - "domain/%{domain}"
    - "env/%{::environment}"
    - "ops/%{operatingsystem}"
    - "os/%{osfamily}"
    - common
:backends:
    - yaml
:yaml:
    :datadir: /etc/puppet/data
Run Code Online (Sandbox Code Playgroud)

对于文件夹结构,您可以使用您可以在 的输出中看到的任何事实facter -y。例如,您可以为每个 CPU 架构提供 hiera 配置文件。然后你会添加行

- "arch/%{::architecture}"
Run Code Online (Sandbox Code Playgroud)

希拉会看看我们说arch/amd64.yaml

要调试 hiera,您可以转储当前事实:

   $ facter -y > myhost.yaml
Run Code Online (Sandbox Code Playgroud)

并寻找一些变量:

   $ hiera -y myhost.yml snmp_location --debug
Run Code Online (Sandbox Code Playgroud)

Hiera 将遍历所有规则并尝试找到变量:

DEBUG: Mon Nov 11 11:00:23 +0100 2013: Hiera YAML backend starting
DEBUG: Mon Nov 11 11:00:23 +0100 2013: Looking up snmp_location in YAML backend
DEBUG: Mon Nov 11 11:00:23 +0100 2013: Looking for data source host/myhost.example.com
...
DEBUG: Mon Nov 11 11:00:23 +0100 2013: Looking for data source ops/Ubuntu
DEBUG: Mon Nov 11 11:00:23 +0100 2013: Cannot find datafile /etc/puppet/data/ops/Ubuntu.yaml, skipping
Run Code Online (Sandbox Code Playgroud)

为了匹配,$::clientcert最好将顶级域提取到一个单独的事实,然后只使用cert/example1.org.yaml包含如下内容的 yaml 文件:

---
snmp_location: 'Example Org 1'
Run Code Online (Sandbox Code Playgroud)

很高兴知道,即使您的模块根本不包含任何 hiera 函数调用,您也可以轻松设置参数值:

class snmp (
  $location = 'foo',
) { 
# ...
}
Run Code Online (Sandbox Code Playgroud)

一些 hiera 配置:

---
snmp::location: 'Example Org 1'
Run Code Online (Sandbox Code Playgroud)