0

How to add same cook book recipe in other recipe

  include_recipe  "localcookbook::test"

where i have included there test recipe variables are not passing

Udhay
  • 7
  • 7
  • Possible duplicate of [Using a variable inside a Chef recipe](http://stackoverflow.com/questions/23551104/using-a-variable-inside-a-chef-recipe) – StephenKing Mar 18 '17 at 09:58

2 Answers2

0

Local variables are not visible across an include because that would make them not local variables. Or more generally because that isn't how Ruby works.

coderanger
  • 52,400
  • 4
  • 52
  • 75
  • Means need to declare global variable, Ok help me .Know I am asking general questions i am beginner as Devops – Udhay Mar 19 '17 at 07:00
0

In order to achieve what you want you need to use cookbook's helpers and libraries. As a start you can check this resources for libraries https://blog.chef.io/2014/03/12/writing-libraries-in-chef-cookbooks/

This is basic examples with helpers.

In your cookbook folder you needs to create libraries/helpers.rb file

module MyCookbook
  module Helpers
    @@state_value ||= ''

    def set_state_value(v)
      @@state_value = v
      @@state_value
    end

    def get_state_value
      @@state_value
    end
  end
end

Chef::Recipe.send(:include, MyCookbook::Helpers)

Lets say that you have two recipes - A and B (executed sequently) in chef.

In A you put set_state_value("state value") and in B get_state_value and you have what you set from A recipe in B recipe.

gsone
  • 1,188
  • 8
  • 25