通过迭代 gem 查询输出创建嵌套的 Puppet 事实 (Ruby)
Creating nested Puppet fact (Ruby) by iterating over gem query output
我正在使用 Ruby 代码来查询 DNS 详细信息并创建 Puppet 自定义事实(puppet 5,Facter 3.11.6),但是我正在尝试修改它以从 key/value 创建嵌套事实查询获得的对。
使用键名设置单个事实的代码是:
require 'resolv'
Resolv::DNS::Config.default_config_hash.each do | key, value |
if !value.nil?
Facter.add("dns_#{key}") do
if value.is_a?(Array)
setcode { value.join(',') }
else
setcode { value }
end
end
end
end
因此创建了个别事实:
dns_nameserver => 192.168.1.1,192.168.1.2
dns_ndots => 1
dns_search => test.domain
到目前为止,我在 'DNS' 的父事实下创建嵌套事实的失败尝试是:
require 'resolv'
Facter.add("dns") do
value ={}
Resolv::DNS::Config.default_config_hash.each do | key, result |
if !result.nil?
if result.is_a?(Array)
setcode { value['#{key}'] = result.join(',') }
else
setcode { value['#{key}'] = result }
end
end
end
end
给出的结果有限:
dns => 1
我尝试过的其他代码似乎将数组输出放入字符串中,并且多个 IP 在方括号内被引号超过 2 行,而不是按照页面顶部的第一个代码块输出。
我试图实现的事实结构(通过修改页面顶部代码)是:
dns => {
nameserver => 192.168.1.1,192.168.1.2,
ndots => 1,
search => test.domain,
}
在此先感谢您的帮助。
在一位 poster 的帮助下,我终于得到了这个,他在这里放置了一些很棒的代码线索,但不幸的是,它很快就被删除了。这是有效的代码:
require 'resolv'
Facter.add(:networking_dns) do
setcode do
Resolv::DNS::Config.default_config_hash.each_with_object({}) do | (key, value), sub|
if !value.nil?
sub[key] = value
sub
end
end
end
end
现在进行一些解释性说明(请随时纠正我或对此提供任何优化):
# the resolv gem is required
require 'resolv'
# create the parent fact (has no value of its own)
Facter.add(:networking_dns) do
# start building instructions in the fact
setcode do
# use the resolv gem to lookup values in /etc/resolv.conf and add .each to process all key/value pairs returned
# also add _with_object({}) and sub in the variables to set a blank value for sub. Saves doing it separately. Sub can be any name but denotes the declaration for the nested facts
Resolv::DNS::Config.default_config_hash.each_with_object({}) do | (key, value), sub|
# create facts only when the value is not nil
if !value.nil?
sub[key] = value
sub
# using a closing blank entry for a nested fact is critical or they won't create! Place this outside of the case statement to prevent blank values
end
end
end
end
# use the appropriate number of ends and indent for readability
感谢在删除指南之前post在此处编辑了指南的人。如果你再次post,我想给你投票。
欢迎任何关于优化可行解决方案的提示,因为我仍在掌握Ruby(为此花费了数小时!)
我正在使用 Ruby 代码来查询 DNS 详细信息并创建 Puppet 自定义事实(puppet 5,Facter 3.11.6),但是我正在尝试修改它以从 key/value 创建嵌套事实查询获得的对。
使用键名设置单个事实的代码是:
require 'resolv'
Resolv::DNS::Config.default_config_hash.each do | key, value |
if !value.nil?
Facter.add("dns_#{key}") do
if value.is_a?(Array)
setcode { value.join(',') }
else
setcode { value }
end
end
end
end
因此创建了个别事实:
dns_nameserver => 192.168.1.1,192.168.1.2
dns_ndots => 1
dns_search => test.domain
到目前为止,我在 'DNS' 的父事实下创建嵌套事实的失败尝试是:
require 'resolv'
Facter.add("dns") do
value ={}
Resolv::DNS::Config.default_config_hash.each do | key, result |
if !result.nil?
if result.is_a?(Array)
setcode { value['#{key}'] = result.join(',') }
else
setcode { value['#{key}'] = result }
end
end
end
end
给出的结果有限:
dns => 1
我尝试过的其他代码似乎将数组输出放入字符串中,并且多个 IP 在方括号内被引号超过 2 行,而不是按照页面顶部的第一个代码块输出。
我试图实现的事实结构(通过修改页面顶部代码)是:
dns => {
nameserver => 192.168.1.1,192.168.1.2,
ndots => 1,
search => test.domain,
}
在此先感谢您的帮助。
在一位 poster 的帮助下,我终于得到了这个,他在这里放置了一些很棒的代码线索,但不幸的是,它很快就被删除了。这是有效的代码:
require 'resolv'
Facter.add(:networking_dns) do
setcode do
Resolv::DNS::Config.default_config_hash.each_with_object({}) do | (key, value), sub|
if !value.nil?
sub[key] = value
sub
end
end
end
end
现在进行一些解释性说明(请随时纠正我或对此提供任何优化):
# the resolv gem is required
require 'resolv'
# create the parent fact (has no value of its own)
Facter.add(:networking_dns) do
# start building instructions in the fact
setcode do
# use the resolv gem to lookup values in /etc/resolv.conf and add .each to process all key/value pairs returned
# also add _with_object({}) and sub in the variables to set a blank value for sub. Saves doing it separately. Sub can be any name but denotes the declaration for the nested facts
Resolv::DNS::Config.default_config_hash.each_with_object({}) do | (key, value), sub|
# create facts only when the value is not nil
if !value.nil?
sub[key] = value
sub
# using a closing blank entry for a nested fact is critical or they won't create! Place this outside of the case statement to prevent blank values
end
end
end
end
# use the appropriate number of ends and indent for readability
感谢在删除指南之前post在此处编辑了指南的人。如果你再次post,我想给你投票。
欢迎任何关于优化可行解决方案的提示,因为我仍在掌握Ruby(为此花费了数小时!)