Search code examples
rubychef-infrachef-recipe

How to call a method from a different recipe?


I am trying to access a method in recipe 'B' of cookbook "b" from recipe 'A' of cookbook "a". I have included the recipe 'B' in recipe 'A' using include_recipe 'cookbook::recipe'.

    #cookbook Flower
    #chef recipe 'Rose' DSL
     def method_to_be_called 
        do something
     end

    #cookbook Animal
    #chef recipe 'Tiger' DSL
    include_recipe "Flower::Rose"
      #call method_to_be_called of 'Rose' recipe
   end

I am learning Ruby and Chef DSL, hence I don't know what I'm trying to achieve is possible or not. If yes, how do I call the method? Thanks in advance.


Solution

  • That isn't really how Chef works. You can wrap your method in a library and call it that way.

    For example, put your method in:

    # cookbook/rose/libraries/helper.rb
    class Rose
       def self.method_to_be_called
       end
    end
    
    # cookbook/tiger/recipes/default.rb
    Rose.method_to_be_called()