在配方中使用库中的类方法

Sba*_*bal 4 chef-infra

我只是想在厨师中创建一本简单的食谱。我正在使用图书馆作为学习过程。

module ABC
  class YumD
    def self.pack (*count)
      for i in 0...count.length
        yum_packag "#{count[i]}" do
          action :nothing
        end.run_action :install
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

当我在配方中调用它时,我收到一个编译错误,上面写着

undefined method `yum_package' for ABC::YumD:Class
Run Code Online (Sandbox Code Playgroud)

set*_*rgo 5

您无权访问库中的 Chef Recipe DSL。DSL 方法实际上只是成熟的 Ruby 类的捷径。例如:

template '/etc/foo.txt' do
  source 'foo.erb'
end
Run Code Online (Sandbox Code Playgroud)

实际上“编译”(读作:“被解释”)为:

template = Chef::Resource::Template.new('/etc/foo.txt')
template.source('foo.erb')
template.run_action(:create)
Run Code Online (Sandbox Code Playgroud)

所以,在你的情况下,你想使用YumPackage

module ABC
  class YumD
    def self.pack(*count)
      for i in 0...count.length
        package = Chef::Resource::YumPackage.new("#{count[i]}")
        package.run_action(:install)
      end
    end
  end
 end
Run Code Online (Sandbox Code Playgroud)