puppet:Hiera不喜欢任何子目录



任何Puppet和Hiera专家可以帮助我解决我所看到的这种烦人的情况吗?

Hiera 适用于common.yaml中的任何内容:

cat common.yaml
---
resolv_conf::nameserver:
- x.x.x.x
- x.x.x.x
profile::ntp::servers:
- x.x.x.x
- x.x.x.x

然后用以下作品进行测试:

hiera resolv_conf::nameservers environment=production
["x.x.x.x", "x.x.x.x"]

因此,上述工作按预期工作。但是,如果我在hieradata中使用nodes目录并在以下位置添加测试数据:

cat nodes/fqdn.yaml
---
profile::ntp::servers:
-1.1.1.1
-2.2.2.2

然后我相应地调整我的hiera.yaml文件:

cat hiera.yaml
---
:backends: yaml
:yaml:
:datadir: "/etc/puppetlabs/code/environments/%{environment}/hieradata"
:hierarchy:
-"nodes/%{::fqdn}"
- "common"
:logger: console
:merge_behavior: native
:deep_merge_options: {}

并将正确的行添加到全局hiera.yaml中(两个文件现在看起来相同以进行测试)。

测试时出现以下错误:

hiera profile::ntp::servers environment=production -d
调试: xxxxx: Hiera YAML 后端启动 调试: xxxxxxx: 查找配置文件::ntp::YAML 后端中的服务器 调试: xxxxx:

忽略 :层次结构中的错误定义: 'nodes/'调试: xxxxx:查找数据源常见
调试: xxxx: 找到配置文件::ntp::服务器共同
["x.x.x.x", "x.x.x.x"]


因此,它会在common中找到值,但不喜欢nodes目录。

我在hiera.yaml中尝试了大量不同的hierarchy线变体,以及我可以想象到的其他任何地方的许多不同选择!

上面可能有错别字,因为我没有复制和粘贴,所以我不得不手动输入所有内容,但我一次又一次地检查实时文件是否存在拼写错误和语法错误。

有什么想法吗?

现在我回家了一些更新,可以复制/粘贴:

这是在全新安装的puppet enterprise上,使用代码管理器和gitlab。

[root@client-01 nodes]# pwd
/root/control-repo/hieradata/nodes
[root@client-01 nodes]# ll
total 8
-rw-r--r--. 1 root root 34 Jul 13 19:04 puppet.example.com.yaml
-rw-r--r--. 1 root root 18 Jul 13 18:58 test.yaml
[root@client-01 nodes]# cat puppet.example.com.yaml
---
message:
- "This is in nodes"

这是在开发机器上,然后推送了存储库,启动了代码部署的钩子,我已经检查了相同的数据是否在 puppet master 上的/etc/puppetlabs/code/environment/production 等中可用。所以代码管理器正在工作。

在傀儡服务器上进行以下测试:

[root@puppet ~]# hostname -f
puppet.example.com
[root@puppet ~]# hiera message environment=production
nil
[root@puppet ~]# hiera message environment=production 
fqdn=puppet.example.com
nil
[root@puppet ~]# hiera message environment=production fqdn=puppet.example.com -d
DEBUG: 2017-07-13 19:28:15 +0100: Hiera YAML backend starting
DEBUG: 2017-07-13 19:28:15 +0100: Looking up message in YAML backend
DEBUG: 2017-07-13 19:28:15 +0100: Ignoring bad definition in :hierarchy: 'nodes/'
DEBUG: 2017-07-13 19:28:15 +0100: Looking for data source common
nil
[root@puppet ~]#

所以它看起来像完全相同的问题:

我的hiera.yaml

[root@puppet ~]# cat /etc/puppetlabs/puppet/hiera.yaml
---
:backends:
- yaml
:hierarchy:
- "nodes/%{::fqdn}"
- common
:yaml:
# datadir is empty here, so hiera uses its defaults:
# - /etc/puppetlabs/code/environments/%{environment}/hieradata on *nix
# - %CommonAppData%PuppetLabscodeenvironments%{environment}hieradata on Windows
# When specifying a datadir, make sure the directory exists.
:datadir: "/etc/puppetlabs/code/environments/%{environment}/hieradata"

这有点令人放心,因为这意味着另一个傀儡服务器可能没有重大问题,而且很可能我做错了什么?

进一步编辑:

所以我昨晚没有太多时间来测试东西,但我确实发现了这个:

看起来问题出在事实变量上,即"%{::fqdn}">

如果我在 hieradata 的根目录中执行以下操作:

[root@client-01 hieradata]# pwd ; ll ; cat test.yaml
/root/control-repo/hieradata
total 12
-rw-r--r--. 1 root root  4 Jul 13 19:25 common.yaml
drwxr-xr-x. 2 root root 52 Jul 13 19:04 nodes
-rw-r--r--. 1 root root 19 Jul 13 21:47 puppet.example.com.yaml
-rw-r--r--. 1 root root 18 Jul 13 21:43 test.yaml
---
name:
- "jon"

然后用hiera name environment=production测试它是否工作正常。

如果我在 hieradata 的根目录中执行以下操作:

[root@client-01 hieradata]# pwd ; ll ; cat puppet.example.com.yaml
/root/control-repo/hieradata
total 12
-rw-r--r--. 1 root root  4 Jul 13 19:25 common.yaml
drwxr-xr-x. 2 root root 52 Jul 13 19:04 nodes
-rw-r--r--. 1 root root 19 Jul 13 21:47 puppet.example.com.yaml
-rw-r--r--. 1 root root 18 Jul 13 21:43 test.yaml
---
test:
- "pass"

然后我通过以下方式进行测试:

[root@puppet ~]# hostname -f ; hiera test environment=production -d
puppet.example.com
DEBUG: 2017-07-13 21:53:44 +0100: Hiera YAML backend starting
DEBUG: 2017-07-13 21:53:44 +0100: Looking up test in YAML backend
DEBUG: 2017-07-13 21:53:44 +0100: Ignoring bad definition in :hierarchy: 
'nodes/'
DEBUG: 2017-07-13 21:53:44 +0100: Ignoring bad definition in :hierarchy: ''
DEBUG: 2017-07-13 21:53:44 +0100: Looking for data source test
DEBUG: 2017-07-13 21:53:44 +0100: Looking for data source common
nil
[root@puppet ~]# hostname -f ; hiera test environment=production 
fqdn=puppet.example.com -d
puppet.example.com
DEBUG: 2017-07-13 21:53:57 +0100: Hiera YAML backend starting
DEBUG: 2017-07-13 21:53:57 +0100: Looking up test in YAML backend
DEBUG: 2017-07-13 21:53:57 +0100: Ignoring bad definition in :hierarchy: 
'nodes/'
DEBUG: 2017-07-13 21:53:57 +0100: Ignoring bad definition in :hierarchy: ''
DEBUG: 2017-07-13 21:53:57 +0100: Looking for data source test
DEBUG: 2017-07-13 21:53:57 +0100: Looking for data source common
nil

因此,完全忽略节点的子文件夹并在 hieradata 的根目录中进行测试表明,具有固定名称的 yaml 文件可以工作,但是当使用 facter 进行每个节点查找时......这就是失败的地方。

[root@puppet ~]# facter fqdn
puppet.example.com
[root@puppet ~]# hostname -f
puppet.example.com

但是,事实似乎工作得很好。

任何帮助将不胜感激,谢谢。 乔恩

我重现了这一点,问题实际上是范围指标需要包含在 hiera 命令行的事实中。

即代替:

# hiera test environment=production fqdn=puppet.example.com -d

你应该说:

# hiera test environment=production ::fqdn=puppet.example.com -d

要在 Rspec-puppet 中重现:

$ cat manifests/init.pp 
class foo (
$servers,
$nameserver,
) {
notify { "servers: $servers": }
notify { "nameserver: $nameserver": }
}

$ cat spec/fixtures/hiera/hiera.yaml 
---
:backends:
- yaml
:hierarchy:
- "nodes/%{::fqdn}"
- common
:yaml:
:datadir: 'spec/fixtures/hieradata'
:logger: console
:merge_behavior: native
:deep_merge_options: {}

$ cat spec/fixtures/hieradata/common.yaml 
---
foo::nameserver:
- 3.3.3.3
- 4.4.4.4
foo::servers:
- 5.5.5.5
- 6.6.6.6

$ cat spec/fixtures/hieradata/nodes/myhost.example.com.yaml 
---
foo::servers:
- 1.1.1.1
- 2.2.2.2

$ cat spec/spec_helper.rb 
require 'puppetlabs_spec_helper/module_spec_helper'
RSpec.configure do |c|
c.hiera_config = 'spec/fixtures/hiera/hiera.yaml'
c.default_facts = {
:fqdn => 'myhost.example.com',
}
end

$ cat spec/classes/test_spec.rb 
require 'spec_helper'
describe 'foo', :type => :class do
it { File.write('myclass.json', PSON.pretty_generate(catalogue)) }
end

编译:

$ bundle exec rake spec 
...
Finished in 0.15682 seconds (files took 0.97868 seconds to load)
1 example, 0 failures

应用:

$ bundle exec puppet apply --environment=rp_env --catalog=myclass.json 
Notice: servers: [1.1.1.1, 2.2.2.2]
Notice: /Stage[main]/Foo/Notify[servers: [1.1.1.1, 2.2.2.2]]/message: defined 'message' as 'servers: [1.1.1.1, 2.2.2.2]'
Notice: nameserver: [3.3.3.3, 4.4.4.4]
Notice: /Stage[main]/Foo/Notify[nameserver: [3.3.3.3, 4.4.4.4]]/message: defined 'message' as 'nameserver: [3.3.3.3, 4.4.4.4]'
Notice: Applied catalog in 0.02 seconds

我在spec_helper中更改了 fqdn 的值,然后编译并再次应用:

$ bundle exec puppet apply --environment=rp_env --catalog=myclass.json 
Notice: servers: [5.5.5.5, 6.6.6.6]
Notice: /Stage[main]/Foo/Notify[servers: [5.5.5.5, 6.6.6.6]]/message: defined 'message' as 'servers: [5.5.5.5, 6.6.6.6]'
Notice: nameserver: [3.3.3.3, 4.4.4.4]
Notice: /Stage[main]/Foo/Notify[nameserver: [3.3.3.3, 4.4.4.4]]/message: defined 'message' as 'nameserver: [3.3.3.3, 4.4.4.4]'
Notice: Applied catalog in 0.01 seconds

(rp_env是 Rspec-puppet 设置的环境的魔法值。

所以代码有效。

但是Hiera命令行有问题。

重现您的错误消息:

$ bundle exec hiera foo::servers environment=rp_env -d -c spec/fixtures/hiera/hiera.yaml 
DEBUG: 2017-07-15 13:21:54 +1000: Hiera YAML backend starting
DEBUG: 2017-07-15 13:21:54 +1000: Looking up foo::servers in YAML backend
DEBUG: 2017-07-15 13:21:54 +1000: Ignoring bad definition in :hierarchy: 'nodes/'
DEBUG: 2017-07-15 13:21:54 +1000: Looking for data source common
DEBUG: 2017-07-15 13:21:54 +1000: Found foo::servers in common
["5.5.5.5", "6.6.6.6"]

尝试使用与命令行相同的语法在命令行上设置事实:

$ bundle exec hiera foo::servers fqdn=myhost.example.com environment=rp_env -d -c spec/fixtures/hiera/hiera.yaml 
DEBUG: 2017-07-15 13:23:34 +1000: Hiera YAML backend starting
DEBUG: 2017-07-15 13:23:34 +1000: Looking up foo::servers in YAML backend
DEBUG: 2017-07-15 13:23:34 +1000: Ignoring bad definition in :hierarchy: 'nodes/'
DEBUG: 2017-07-15 13:23:34 +1000: Looking for data source common
DEBUG: 2017-07-15 13:23:34 +1000: Found foo::servers in common
["5.5.5.5", "6.6.6.6"]

最后提供缺少的范围指示器:

$ bundle exec hiera foo::servers ::fqdn=myhost.example.com environment=rp_env -d -c spec/fixtures/hiera/hiera.yaml 
DEBUG: 2017-07-15 13:24:09 +1000: Hiera YAML backend starting
DEBUG: 2017-07-15 13:24:09 +1000: Looking up foo::servers in YAML backend
DEBUG: 2017-07-15 13:24:09 +1000: Looking for data source nodes/myhost.example.com
DEBUG: 2017-07-15 13:24:09 +1000: Found foo::servers in nodes/myhost.example.com
["1.1.1.1", "2.2.2.2"]

万岁,它有效。

同意这很令人困惑。

另请参阅此处。

在你的hiera.yaml中,你有:

-"nodes/%{::fqdn}"

在那里,%{::fqdn}指的是事实事实fqdn

同时,您创建了一个名为nodes/fqdn.yaml的文件。

Hiera 不会看到该文件,除非您fqdn事实实际上是返回fqdn(而不是像myhost.example.com这样的东西)。

如果您改为说,它应该找到该文件:

hiera profile::ntp::servers environment=production fqdn=fqdn -d

但是,您可能想做的是重命名 Hiera 文件以匹配节点的实际 FQDN,即

cat nodes/myhost.example.com.yaml
---
profile::ntp::servers:
-1.1.1.1
-2.2.2.2

调试输出中的关键信息是:

调试:xxxxx:忽略 :hierarchy 中的错误定义:"节点/">

这应该表示该查找优先级的 YAML 层次结构存在语法错误。果然,如果我们使用像 http://www.yamllint.com 这样的在线验证工具,我们会看到它有一个语法错误。将 hieradata 文件更改为:

# nodes/myhost.example.com.yaml
---
profile::ntp::servers:
- 1.1.1.1
- 2.2.2.2

它应该工作正常。

我建议将来使用工具或一些代码来验证代码和数据,以便您可以更快、更轻松地识别这些问题。我为此写了一个流行的,但不会插入它,因为我认为 StackOverflow 可能对此不满意。

最新更新