为什么 Hiera 找不到环境,Puppet 也找不到

为什么 Hiera 找不到环境,Puppet 也找不到

鉴于这些文件:

# find /etc/puppetlabs/code -type f | grep -v modules | xargs head -n 100
==> /etc/puppetlabs/code/environments/production/hieradata/common.yaml <==
pgwatch:
  password: "mypass1"
puppetdb:
  password: "mypass2"

==> /etc/puppetlabs/code/environments/production/manifests/site.pp <==
node "todd.ca.seevibes.com" {
  class { 'postgresql::globals':
    encoding            => 'UTF-8',
    locale              => 'en_US.UTF-8',
    manage_package_repo => true,
    version             => '9.1',
  } -> class{'postgresql::server':
  } -> postgresql::server::db{'puppetdb':
    user     => 'puppetdb',
    password => postgresql_password('puppetdb', hiera('puppetdb::password')),
  } -> postgresql::server::db{'pgwatch':
    user     => 'pgwatch',
    password => postgresql_password('pgwatch', hiera('pgwatch::password')),
  }

  postgresql::server::pg_hba_rule{'allow pgwatch from anywhere':
    address     => '0.0.0.0/32',
    auth_method => 'md5',
    database    => 'pgwatch',
    user        => 'pgwatch',
  }
}

==> /etc/puppetlabs/code/hiera.yaml <==
---
:backends:
  - json
  - yaml
:yaml:
  # Use the default value for datadir
  :datadir:
:json:
  # Use the default value for datadir
  :datadir:
:hierarchy:
  - "node/%{::fqdn}"
  - "node/%{::hostname}"
  - "%{::domain}"
  - common

我期望以下内容返回pgwatch::password值:

# hiera --debug pgwatch::password
DEBUG: 2015-12-09 22:35:06 +0000: Hiera JSON backend starting
DEBUG: 2015-12-09 22:35:06 +0000: Looking up pgwatch::password in JSON backend
DEBUG: 2015-12-09 22:35:06 +0000: Looking for data source common
DEBUG: 2015-12-09 22:35:06 +0000: Cannot find datafile /etc/puppetlabs/code/environments//hieradata/common.json, skipping
DEBUG: 2015-12-09 22:35:06 +0000: Hiera YAML backend starting
DEBUG: 2015-12-09 22:35:06 +0000: Looking up pgwatch::password in YAML backend
DEBUG: 2015-12-09 22:35:06 +0000: Looking for data source common
DEBUG: 2015-12-09 22:35:06 +0000: Cannot find datafile /etc/puppetlabs/code/environments//hieradata/common.yaml, skipping
nil

Puppet 的相同查询也会失败:

# puppet agent -t
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Evaluation Error: Error while evaluating a Function Call, Could not find data item puppetdb::password in any Hiera data file and no default supplied at /etc/puppetlabs/code/environments/production/manifests/site.pp:10:49 on node todd.ca.seevibes.com
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run

在 hiera 的调试日志中,我们可以清楚地看到搜索路径中缺少的环境:(注意之前的/etc/puppetlabs/code/environments//hieradata/common.json双重)。//hieradata

我已经发现如何在命令行上向 Hiera 指定 $environment?最佳答案是传递::environment=production,这不会改变任何事情。当我传递 时environment=production,查找“成功”但不返回任何内容:

# hiera --debug pgwatch::password 'environment=production'
DEBUG: 2015-12-09 22:42:12 +0000: Hiera JSON backend starting
DEBUG: 2015-12-09 22:42:12 +0000: Looking up pgwatch::password in JSON backend
DEBUG: 2015-12-09 22:42:12 +0000: Looking for data source common
DEBUG: 2015-12-09 22:42:12 +0000: Cannot find datafile /etc/puppetlabs/code/environments/production/hieradata/common.json, skipping
DEBUG: 2015-12-09 22:42:12 +0000: Hiera YAML backend starting
DEBUG: 2015-12-09 22:42:12 +0000: Looking up pgwatch::password in YAML backend
DEBUG: 2015-12-09 22:42:12 +0000: Looking for data source common
nil

我猜测这一次,路径是正确的,并且找到了数据文件,但没有返回值。

我原本希望 Puppet 运行来找到该值。我做错了什么?

# uname -a
Linux todd 3.10.23-xxxx-grs-ipv6-64 #1 SMP Mon Dec 9 16:02:37 CET 2013 x86_64 x86_64 x86_64 GNU/Linux
# puppet --version
4.3.1
# hiera --version
3.0.5
# puppet agent --configprint environment
production
# which puppet
/opt/puppetlabs/bin/puppet
# which hiera
/opt/puppetlabs/bin/hiera

答案1

在你的文件中/etc/puppetlabs/code/environments/production/hieradata/common.yaml你需要写puppetdb::password: mypass2

在 yaml 语法中,这是一个哈希,而不是一个简单的变量

puppetdb:
  password: "mypass2"

这样你就有一个简单的变量

puppetdb::password: mypass2

相关内容