简体   繁体   中英

Using global variables in chef ruby

Is it possible to define some variable as global in a chef recipe.Like I am declaring & changing a var in a chef resource and later i want to use this changed value in the recipe code.

version=""
ruby 'extract_module' do
    #cwd ::File.dirname(C:\\chef\\cache)
    code <<-EOH
        require 'win32/registry'
        Win32::Registry::HKEY_LOCAL_MACHINE.open(
        'Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall') do |reg|
            reg.each_key do |key|
                k = reg.open(key)
                app = k["DisplayName"] rescue nil

                if app == "Qlik Sense 3.2"
                    version = k["DisplayVersion"] rescue nil
                    puts version
                end
            end
        end
    EOH
end

puts "#{version}"

This variable is not printing outside the ruby resource. Can we do something like python, declare with global keyword & modify the value. Any similar here?

The ruby resource doesn't run in the same process, it runs ruby -E as a subprocess. What you want is a ruby_block resource, which is unrelated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM