puppet - 在 puppet 中使用多个 hiera.yaml 文件

标签 puppet hiera

在将 Debian 作为我们的 Ganeti 环境中的硬件操作系统和虚拟机操作系统引入我们的基础架构之后,我现在正在尝试使用本地 hiera.yaml 为 Debian 主机部署 apt 源列表。文件在它自己的模块中。

我们正在部署适用于 Ubuntu 的 apt 源列表以及我们的本地存储库,其中包含一个专用模块作为 puppetlabs/apt 模块的包装器。全局hiera.yaml在 puppet 服务器上看起来如下:

---
version: 5
defaults:
  datadir: data
  data_hash: yaml_data
hierarchy:
  - name: "module scope"
    paths:
      - "%{facts.fqdn}.yaml"
      - "%{facts.context}-%{facts.location}-%{facts.hostgroup}.yaml"
      - "%{facts.context}-%{facts.datacenter}-%{facts.hostgroup}.yaml"
      - "%{facts.context}-%{facts.hostgroup}.yaml"
      - "%{facts.context}-%{facts.location}.yaml"
      - "%{facts.context}-%{facts.datacenter}.yaml"
      - "%{facts.context}.yaml"
      - common.yaml
    datadir: "/etc/puppetlabs/code/environments/%{environment}/modules/%{module_name}/data"

apt_sources模块common.yaml包含我们 repo 的 apt key 。 %{facts.context}.yaml包含所有 Ubuntu 和我们的 repo 源列表,这在大多数情况下就足够了,因此对于某些主机组,我们需要一些外部 repo ,例如 mysql , percona , ceph等.. 这些来源包含在各自的 yaml 文件中,或者在 %{facts.context}-%{facts.hostgroup}.yaml 中。或其他 yaml 文件,最后我们只是合并 %{facts.context}.yaml 中的哈希值以及其他相关的 yaml 文件。 现在有了 Debian,事情变得有点复杂,我不得不重组 data我们的目录 apt_sources模块,因此 Debian 源列表与 Ubuntu 源列表分开如下:

apt_sources$ tree -L 1 data/
data/
├── common.yaml
├── Debian
└── Ubuntu

2 directories, 1 file
apt_sources$ 

然后我创建了一个本地 hiera.yaml包含以下内容的文件:

---
version: 5
defaults:
  datadir: data
  data_hash: yaml_data
hierarchy:
  - name: "module scope"
    paths:
      - "%{facts.operatingsystem}/%{facts.fqdn}.yaml"
      - "%{facts.operatingsystem}/%{facts.context}-%{facts.location}-%{facts.hostgroup}.yaml"
      - "%{facts.operatingsystem}/%{facts.context}-%{facts.datacenter}-%{facts.hostgroup}.yaml"
      - "%{facts.operatingsystem}/%{facts.context}-%{facts.hostgroup}.yaml"
      - "%{facts.operatingsystem}/%{facts.context}-%{facts.location}.yaml"
      - "%{facts.operatingsystem}/%{facts.context}-%{facts.datacenter}.yaml"
      - "%{facts.operatingsystem}/%{facts.context}.yaml"
      - common.yaml
    datadir: "/etc/puppetlabs/code/environments/%{environment}/modules/%{module_name}/data"

我们init.pp的相关部分由于与某些 QA 基础设施的兼容性,它必须保持 puppet 3 兼容:

#
class apt_sources (
  Hash $gnupg_key     = {},
  Hash $pin           = {},
  $proxy              = {},
  $purge_sources      = false,
  Hash $settings      = {},
  Hash $sources       = {},
  ) {

  class { 'apt':
    update => {
      frequency => 'daily',
    },
    purge  => {
      'sources.list'   => $purge_sources,
      'sources.list.d' => $purge_sources,
    },
  }

  create_resources('apt::source', hiera_hash('apt_sources::sources', $sources))
  create_resources('apt::setting', hiera_hash('apt_sources::settings', $settings))
  create_resources('apt::key', hiera_hash('apt_sources::gnupg_key', $gnupg_key))
  create_resources('apt::pin', hiera_hash('apt_sources::pin', $pin))

  Apt::Pin <| |> -> Apt::Source <| |> -> Apt::Ppa <| |> -> Exec['apt_update'] -> Package <| |>
}

现在,在为主机部署 apt_sources 时附加 %{facts.context}-%{facts.hostgroup}.yaml文件,源列表不会合并,而只有更具体的 yaml 文件获胜,在本例中为 %{facts.context}-%{facts.hostgroup}.yaml文件,所以主要 repo 在%{facts.context}.yaml未部署。 在 puppetserver 中,我可以在日志文件中看到 Puppet 如何使用全局 hiera.yaml 查找 key 然后是本地 hiera.yaml但仅针对第一个哈希,然后是这一行:

Hiera configuration recreated due to change of scope variables used in interpolation expressions

Puppet 一直在寻找其他 key ,但这次只使用全局 hiera.yaml配置并跳过本地配置,因此 Puppet 无法找到任何哈希并使用默认值 {}值(value)。

不幸的是我不能替换hiear_hashlookup由于 Puppet 3 的兼容性,目前功能正常。

编辑

最初只有 Ubuntu 作为操作系统,我在目录 data/ 中拥有所有 hiera 数据和 init.pp看起来像这样:

#
class apt_sources (
  $proxy         = {},
  $purge_sources = false,
  $merge_sources = true,
  ) {

  class { 'apt':
    update => {
      frequency => 'daily',
    },
    purge  => {
      'sources.list'   => $purge_sources,
      'sources.list.d' => $purge_sources,
    },
  }

  if $merge_sources {
    $sources = hiera_hash('apt_sources::sources', {})
    create_resources('apt::source', $sources)
  }
  else {
    $sources = hiera('apt_sources::sources')
    create_resources('apt::source', $sources)
  }

  $settings = hiera_hash('apt_sources::settings', {})
  create_resources('apt::setting', $settings)

  $gnupg_key = hiera_hash('apt_sources::gnupg_key', {})
  create_resources('apt::key', $gnupg_key)

  $pin = hiera_hash('apt_sources::pin', {})
  create_resources('apt::pin', $pin)

  Apt::Pin <| |> -> Apt::Source <| |> -> Apt::Ppa <| |> -> Exec['apt_update'] -> Package <| |>
}

也许有人可以解释这种行为。

感谢您的帮助。

最佳答案

我通过将以下内容添加到 common.yaml 来修复它:

lookup_options:
  apt_sources::sources:
    merge:
      strategy: deep

此外,我还更改了 create_resources 语句,如下所示:

create_resources('apt::source', $sources)
create_resources('apt::setting', $settings)
create_resources('apt::key', $gnupg_key)
create_resources('apt::pin', $pin)

关于puppet - 在 puppet 中使用多个 hiera.yaml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55933987/

相关文章:

ruby - 检查 Hiera 值是否存在,如果存在,则将每个值分配给变量

puppet - 在Puppet中,如何访问已定义类型内的变量/属性?

linux - 从第 3 方二进制包构建 RPM

docker - Vagrant , docker , puppet , Chef

automation - 评估来自 hiera 的 puppet list 中的哈希值

yaml - 使用嵌套键在 puppet list 中获取 hiera 值

puppet - 如何在Windows上使用puppet脚本设置JAVA_HOME?

scalability - Scaling Puppet - 什么时候对 WEBrick 来说太多了?

ruby - Puppet hiera 和 create_resource 问题

puppet - 希拉命令行 : How do I view all data in the hierarchy for a given node?