簡體   English   中英

在 puppet 中使用多個 hiera.yaml 文件

[英]using multiple hiera.yaml files with puppet

在將 Debian 作為硬件操作系統以及我們 Ganeti 環境中的 VM 引入我們的基礎設施之后,我現在正在嘗試通過使用模塊中的本地hiera.yaml文件為 Debian 主機部署 apt 源列表。

我們正在為 Ubuntu 以及我們的本地存儲庫部署 apt 源列表,並使用專用模塊作為 puppetlabs/apt 模塊的包裝器。 puppet 服務器上的全局hiera.yaml如下所示:

---
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 密鑰。 %{facts.context}.yaml包含了所有Ubuntu和我們的回購來源名單,這是moste情況下已經足夠,因此,對於我們需要一些外部回購,比如一些主機組mysqlperconaceph等。而這些來源包含在各自的 yaml 文件中,要么在%{facts.context}-%{facts.hostgroup}.yaml要么在其他 yaml 文件中,最后我們只合並%{facts.context}.yaml的哈希並在其他相關的 yaml 文件中。 現在 Debian 的事情變得有點復雜了,我不得不在我們的apt_sources模塊中重組data目錄,以便 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"

由於與某些 QA 基礎設施的兼容性,我們的init.pp的相關部分必須保持 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 <| |>
}

現在,當使用額外的%{facts.context}-%{facts.hostgroup}.yaml文件為主機部署 apt_sources 時,源列表不會被合並,而只有更具體的 yaml 文件獲勝,在這種情況下, %{facts.context}-%{facts.hostgroup}.yaml文件,因此未部署%{facts.context}.yaml中的主要存儲庫。 在 puppetserver 中,我可以在日志文件中看到 Puppet 如何使用全局hiera.yaml和本地hiera.yaml查找鍵,但僅用於第一個哈希,然后是這一行:

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

並且 Puppet 一直在尋找其他鍵,但這次僅使用全局hiera.yaml配置並跳過本地配置,因此 Puppet 找不到任何哈希並使用默認{}值。

不幸的是,由於 Puppet 3 兼容性,我暫時無法用lookup功能替換hiear_hash

編輯

最初只有 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM