Search code examples
rubychef-infrachef-recipe

Chef : Pass parameters to a ruby_block in Chef


How do you pass parameters to a ruby_block in chef.

If I have

  notifies :create, "ruby_block[createErb]", :immediately

and I want pass a parameter (fileToConvert) to this ruby_block (createErb) at the time that I notify.

ruby_block "createErb" do
  block do
    ErbCreator.new(fileToConvert)
  end
  action :nothing
end

How would I do this?


Solution

  • Short answer - you can't.

    RubyBlock is a Chef resource, so it does not accept arbitrary parameters. In your example, I would recommend creating a Chef Extension (LWRP or HWRP):

    In your resource:

    # resources/erb_create.rb
    actions :create
    default_action :create
    
    attribute :filename, name_attribute: true
    # more attributes
    

    And in your provider:

    # providers/erb_create.rb
    action(:create) do
      ErbCreator.new(new_resource.filename)
      # ... etc
    end
    

    Then in a recipe:

    # recipes/default.rb
    cookbook_erb_create 'filename'
    

    You can read more about LWRPs on the Chef Docs.