Using class methods from libraries in a recipe

I'm just trying to create a simple cookbook in a chef. I use libraries as a learning process.

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

When I call this in the recipe, I get a compilation error that says

undefined method `yum_package' for ABC::YumD:Class
+4
source share
2 answers

You do not have access to the chef's recipes library internal libraries. DSL methods are actually just shortcuts to full-sized Ruby classes. For example:

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

Actually "compiles" (reading: "interpreted"):

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

So in your case you want to use 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
+4
source

sethvargo, undefined method events for nil:NilClass: run_context :

module ABC
  class YumD
    def self.pack(*count)
      for i in 0...count.length
        package = Chef::Resource::YumPackage.new("#{count[i]}", run_context)
        package.run_action(:install)
      end
    end
  end
 end
+2

Source: https://habr.com/ru/post/1527896/


All Articles