chef-infra - 在配方中使用库中的类方法

标签 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

当我在配方中调用它时,我收到一个编译错误,上面写着
undefined method `yum_package' for ABC::YumD:Class

最佳答案

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

template '/etc/foo.txt' do
  source 'foo.erb'
end

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

template = Chef::Resource::Template.new('/etc/foo.txt')
template.source('foo.erb')
template.run_action(:create)

所以,在你的情况下,你想使用 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

关于chef-infra - 在配方中使用库中的类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21908433/

相关文章:

ruby - 为什么 Puppet 和 Chef 使用 Ruby?

arrays - Chef 使用数组中的变量

ruby - 如何捕获 Chef 异常

c# - 手工制作的 Chef API 请求 - 获取 "invalid signature for user"

chef-infra - 我可以为 Chef 节点设置fqdn吗?

chef-infra - 我可以引用多个版本的 Chef Recipe 吗?

chef-infra - 如何在 Chef Recipe 文件中附加内容?

python - 以编程方式覆盖 Chef 中的属性

ruby - 如何使用Chef在ruby中编写elasticsearch状态检查代码?

chef-infra - 覆盖配方中的属性