执行puppet agent -t报错:Function lookup()未找到'classes'的值
问题描述
部署一款整合了hieradata的产品时,仅在/opt/<product>/hieradata/data/roles/all_in_one.yaml中添加了自定义配置文件,内容如下:
--- classes: - java - mysql::client - mysql::server - resource_tree
但执行/opt/puppetlabs/bin/puppet agent -t时,抛出如下错误:
2024-07-30T17:24:56.094Z ERROR [qtp1481164360-36] [puppetserver] Puppet Function lookup() did not find a value for the name 'classes' 2024-07-30T17:24:56.095Z ERROR [qtp1481164360-36] [puppetserver] Puppet Function lookup() did not find a value for the name 'classes' on node ip-172-xxxxxx.compute.internal 2024-07-30T17:24:56.095Z ERROR [qtp1481164360-36] [puppetserver] Puppet Server Error: Function lookup() did not find a value for the name 'classes' on node ip-172-xxxxxx.compute.internal
此前未将该hieradata集成到/etc/puppetlabs/code/environments/production的manifests、modules、hiera.yaml和data目录时,同步可正常运行:
[root@ip-172-xxxxxx ~]# sudo /opt/puppetlabs/bin/puppet agent -t Info: Using environment 'production' Info: Retrieving pluginfacts Info: Retrieving plugin Info: Retrieving locales Info: Caching catalog for ip-172-xxxxxx.compute.internal Info: Applying configuration version '1721921132' Notice: Applied catalog in 0.01 seconds
核心原因
问题出在Hiera配置未正确加载新增的all_in_one.yaml文件,或者文件层级匹配规则不生效,导致Puppet无法lookup到classes键值。
排查与解决步骤
1. 检查Hiera配置文件(hiera.yaml)
打开/etc/puppetlabs/code/environments/production/hiera.yaml,确认:
hierarchy部分是否包含roles层级,且路径规则能匹配到你的文件。示例正确配置:
hierarchy: - name: "Roles" path: "roles/%{::role}.yaml" # 保留其他原有层级配置
这里的%{::role}需要和节点fact中的role值对应,才能加载roles/all_in_one.yaml。
datadir是否指向自定义hieradata目录/opt/<product>/hieradata/data/,或是否通过paths直接指定了该文件路径。
2. 验证节点fact值匹配度
如果hiera.yaml中使用了%{::role}变量,执行以下命令查看节点的rolefact值:
/opt/puppetlabs/bin/facter role
若输出不是all_in_one,需修改节点fact值,或调整hierarchy中的变量规则,或重命名yaml文件适配现有fact值。
3. 检查文件权限与YAML格式
- 确保Puppetserver进程能读取该文件:
chown puppet:puppet /opt/<product>/hieradata/data/roles/all_in_one.yaml chmod 644 /opt/<product>/hieradata/data/roles/all_in_one.yaml
- 校验YAML语法是否正确:
# 先安装校验工具(以yum为例) yum install -y yamllint yamllint /opt/<product>/hieradata/data/roles/all_in_one.yaml
若存在缩进错误、语法问题,需修正后再重试。
4. 测试Hiera lookup有效性
在Puppetserver上直接测试lookupclasses值,确认是否能加载到目标文件内容:
/opt/puppetlabs/bin/puppet lookup classes --node ip-172-xxxxxx.compute.internal --environment production
若返回配置中的classes列表,说明配置生效;若仍报错,回到步骤1-2重新检查层级规则。
5. 验证Puppet环境配置
检查/etc/puppetlabs/code/environments/production/environment.conf,确保未限制Hiera加载路径,且正确包含自定义hieradata目录。
验证解决
完成上述调整后,重新执行同步命令:
/opt/puppetlabs/bin/puppet agent -t
配置正确的情况下,即可正常加载catalog并应用hieradata中的配置变更。
内容的提问来源于stack exchange,提问作者Gabriel Dias

