Ruby Hash未填充

mcdcgff0  于 12个月前  发布在  Ruby
关注(0)|答案(3)|浏览(140)

我正在Chef中工作,试图用网络设备信息创建/填充ruby哈希,如nmcli填充的那样。我认为代码是正确的,因为VS Code没有抱怨,它似乎在chef-shell -z中运行得很好,但我不能像我期望的那样查询Ruby Hash,我真的开始失去理智了。
新鲜的眼睛和任何Maven的帮助在这里表示赞赏,谢谢!

interfaces = Hash.new
#DEVICE,TYPE
dev = Mixlib::ShellOut.new("nmcli -terse -field device,type device").run_command.stdout.split(/\n/)
dev.each do |output|
  if "#{output.split(":")[1]}" == 'ethernet'
    interfaces["ethernet" => "#{output.split(":")[0]}"]
  elsif "#{output.split(":")[1]}" == 'wifi'
    interfaces["wifi" => "#{output.split(":")[0]}"]
  else
    Chef::Log.debug("Interface #{output.split(":")} is not supported")
  end
end
chef (17.6.18)>  
 => ["wlp61s0:wifi", "enp0s31f6:ethernet", "lo:loopback"] 
node[interfaces] #nil
node[:interfaces] #nil
node['interfaces'] #nil
node["interfaces"] #nil

当我尝试编辑代码时,如BroiSatse所建议的那样,
这一行interfaces["wifi" => "#{output.split(":")[0]}"]返回存储在键{"wifi" => "#>{output.split(":")[0]}"}下的哈希值。它不执行任何赋值,并且>很可能返回nil。
你需要的是:
interfaces["wifi"] ="#{output.split(":")[0]}"
所以我尝试了,但我仍然从哈希得到一个nil响应。下面是Chef的输出/错误:

chef (17.6.18)> interfaces = Hash.new
chef > #DEVICE,TYPE
chef (17.6.18)> dev = Mixlib::ShellOut.new("nmcli -terse -field device,type device").run_command.stdout.split(/\n/)
 => ["wlp61s0:wifi", "enp0s31f6:ethernet", "lo:loopback"] 
chef > dev.each do |output|
chef >   if "#{output.split(":")[1]}" == 'ethernet'
chef >     interfaces["ethernet"] = "#{output.split(":")[0]}"
chef >   elsif "#{output.split(":")[1]}" == 'wifi'
chef >     interfaces["wifi"] = "#{output.split(":")[0]}"
chef >   else
chef >     Chef::Log.debug("Interface #{output.split(":")} is not supported")
chef >   end
chef (17.6.18)> end
 => ["wlp61s0:wifi", "enp0s31f6:ethernet", "lo:loopback"] 
chef (17.6.18)> node[interfaces] #nil
 => nil 
chef (17.6.18)> node[:interfaces][:ethernet] #nil
(irb):95:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
    from /opt/chef/embedded/lib/ruby/gems/3.0.0/gems/chef-17.6.18/lib/chef/shell.rb:93:in `block in start'
    from /opt/chef/embedded/lib/ruby/gems/3.0.0/gems/chef-17.6.18/lib/chef/shell.rb:92:in `catch'
    from /opt/chef/embedded/lib/ruby/gems/3.0.0/gems/chef-17.6.18/lib/chef/shell.rb:92:in `start'
    from /opt/chef/embedded/lib/ruby/gems/3.0.0/gems/chef-bin-17.6.18/bin/chef-shell:31:in `<top (required)>'
    from /usr/bin/chef-shell:158:in `load'
    from /usr/bin/chef-shell:158:in `<main>'
chef (17.6.18)> node['interfaces'] #nil
chef (17.6.18)> node["interfaces"] #nil
 => nil 
chef (17.6.18)>

更新:2022年05月02日星期一12:08 PST
当我执行此命令时,我可以看到Hash中有数据...但所有实际查询数据的尝试都失败了我不知道我做错了什么:

chef (17.6.18)> puts "#{interfaces}"
{"wifi"=>"wlp61s0", "ethernet"=>"enp0s31f6"}
 => nil 
chef (17.6.18)>
4zcjmb1e

4zcjmb1e1#

就叫

interfaces["ethernet"]

interfaces["wifi"]
interfaces = {}

dev =
  Mixlib::ShellOut.new("nmcli -terse -field device,type device").
    run_command.
    stdout.
    lines(chomp: true)

dev.each do |output|
  device, type_device = output.split(":")

  case type_device
  when "ethernet", "wifi"
    interfaces[type_device] = device
  else
    Chef::Log.debug("Interface #{output} is not supported")
  end
end

注意:只有一个关键的wifi和以太网。因此,如果您有更多设备,则仅使用最后一个值

ryevplcw

ryevplcw2#

因为你是在Chef中运行的,所以你可以使用Ohai收集的自动属性,而不是运行命令来获取它的stdout
Ohai是一个收集系统配置数据的工具,然后将其提供给Chef Infra Client用于烹饪书。
有关机器网络配置的详细信息存储在node['network']['interfaces']哈希下。
在你的食谱中试试这个,看看有什么细节被捕获:

pp node['network']['interfaces']

我们可以将这些自动属性与select过滤器结合使用,以获得网络设备名称,而无需循环。

interfaces = Hash.new

# exclude any devices that don't have a "type", such as loopback
ifaces = node['network']['interfaces'].select { |k, v| ! v['type'].nil? }

# select device names for devices of type "en" (ethernet)
interfaces['ethernet'] = ifaces.select { |k, v| v['type'].match(/^en/) }.keys.first

# select device names for devices of type "wl" (wireless/wifi)
interfaces['wifi'] = ifaces.select { |k, v| v['type'].match(/^wl/) }.keys.first

上面的代码将获得分别匹配enwl的“第一个”设备。如果有多个设备属于同一类型,则可以删除.first,并且整个设备列表将可用。

d4so4syb

d4so4syb3#

下面的代码行返回存储在hsh中{"wifi" => "#{output.split(":")[0]}"}键下的值。它不执行任何赋值,很可能返回nil

interfaces["wifi" => "#{output.split(":")[0]}"]

你需要的是:

interfaces["wifi"] = "#{output.split(":")[0]}"

相关问题