1

我将属性列表设置为 ohai,如下所示。

Ohai.plugin(:mycustom) do
    provides "mycustom"

    collect_data do
        configs = ["sss=fdf", "tryet=werw"]
        Ohai::Log.info("Adding #{configs.length} ohai parameters..........................")
        configs.each { |param|
            if param.to_s.strip.length != 0
                key_value_pair = param.split("=").map(&:strip)
                mycustom Mash.new
                mycustom["mycustom_#{key_value_pair[0].downcase}"] = "#{key_value_pair[1]}"
            end
        }
    end
end

我将运行列表配置为依次运行 ohai 和我的食谱。如何在我的食谱模板中访问上述设置属性?

<%= node['mycustom_sss'] %>

似乎不起作用。

如果我ohai | grep mycustom在运行列表运行后执行它不会返回任何内容。

4

1 回答 1

0

Your plugin provides mycustom so the new Mash and it's values will reside under node['mycustom']. Your example would result in node['mycustom']['mycustom_key']

I can see one issue where you are replacing the mycustom Mash on each iteration of the loop so you would only ever end up with the one final value, but you should still have the one.

As you already get the prefix node['mycustom'] via provide 'mycustom' you can house the attributes directly underneath that rather then building a string including mycustom for the key.

Ohai.plugin(:Mycustom) do
  provides 'mycustom'

  collect_data do

    mycustom Mash.new
    configs = [ "sss=fdf", "tryet=werw" ]
    Ohai::Log.info "Adding #{configs.length} ohai parameters......"

    extract_string_key_values(configs).each do |key,val| 
      Ohai::Log.debug "Got key [#{key}] val [#{val}]"
      next if key.length == 0
      mycustom[key.downcase] = val
    end

  end


  def extract_string_key_values array
    #  Split the array values on = and strip whitespace from all elements 
    array.map{|keyval| keyval.split('=').map(&:strip) }
  end

end

This is ohai 7 but they're not vastly different. I split out the key/val parsing into a separate method so the loop is cleaner.

For command line ohai to pick up the plugin, you need to give it a directory to look in, unless you install the plugin in the original ruby ohai gem path.

ohai -d /your/ohai/dir | grep -A3 mycustom
[2014-09-04T21:00:32+01:00] INFO: Adding 2 ohai parameters...
  "mycustom": {
    "sss": "fdf",
    "tryet": "werw"
  }

Then they would then appear like so in your node structure:

node[:mycustom][:sss] = fdf
node[:mycustom][:tryet] = "werw"

After a chef run you should be able to see the nodes mycustom attributes with knife

knife node show <nodename> -a mycustom
于 2014-09-04T20:35:07.197 回答