如何从hiera_hash生成不含嵌套数据的指定键名数组?
问题:从Hiera哈希中提取顶级键生成数组
原始Hiera哈希结构:
signalfx_custom_receivers: smartagent/http1: type: http host: "es.mydomain.tv" useHTTPS: true smartagent/http2: type: http host: "es.other.tv" useHTTPS: true smartagent/http3: type: http host: "pmd-akamai.cdn.mydomain.tv" useHTTPS: true
需要生成仅包含顶级键的数组,目标格式:
[smartagent/http1, smartagent/http2, smartagent/http3]
尝试的代码:
$signalfx_custom_pipeline = Array($signalfx_custom_receivers)
得到的错误结果:
[["smartagent/http1", {"type"=>"http", "host"=>"es.mydomain.tv", "useHTTPS"=>true}], ["smartagent/http2", {"type"=>"http", "host"=>"es.other.tv", "useHTTPS"=>true}], ["smartagent/http3", {"type"=>"http", "host"=>"pmd-akamai.cdn.mydomain.tv", "useHTTPS"=>true}]]
解决方案
使用Puppet的keys()函数提取哈希的顶级键,直接生成目标数组:
$signalfx_custom_pipeline = $signalfx_custom_receivers.keys()
解释:keys()函数会直接返回哈希所有顶级键的数组,完全匹配需求。而直接用Array()转换哈希时,会将每个键值对转为包含键和对应值的子数组,这就是之前得到嵌套结果的原因。
内容的提问来源于stack exchange,提问作者neilH
相关产品推荐
相关产品推荐

