Variables in recipes


I have an issue when using variables in recipes.

I created a custom resource, which i use from a recipe. Within the recipe i have defined a variable. The custom resource has a property which has the same name as the variable. This seems to give a conflict.

How can i prevent that, when using variables the conflict with a resource? As i do not know all possible properties this can always happen (i assume).

resource_name :minimal

property :name, String, name_property: true
property :libj, [Array, String], required: true

action :install do
  # TODO: check if libj property works as intended.
  sid = node['logcollector']['sid'].upcase
  sid_d = sid.downcase 
  sidadm = "#{sid_d}adm"
  rlibj = [*new_resource.libj]
  adapterpath = "/hana/shared/#{sid}/streaming/cluster/#{sid_d}/adapters"

  rlibj.each do |l|
    print "#{l}\n"
libj = 'This collides with libj in minimal resource'

minimal 'rtparseradapter' do
  libj [ 
  action :install
  notifies :restart, 'etdproject[proxy_tle]', :immediately

etdproject 'proxy_tle' do
  action :nothing
wrong number of arguments (given 3, expected 1..2)

Cookbook Trace:
  /etc/chef/local-mode-cache/cache/cookbooks/ETD-LogCollector/recipes/minimal.rb:11:in `[]'
  /etc/chef/local-mode-cache/cache/cookbooks/ETD-LogCollector/recipes/minimal.rb:11:in `block in from_file'
  /etc/chef/local-mode-cache/cache/cookbooks/ETD-LogCollector/recipes/minimal.rb:10:in `from_file'

Relevant File Content:

  4:  #
  5:  # Copyright:: 2018, The Authors, All Rights Reserved.
  6:  #
  8:  libj = 'This collides with libj in minimal resource'
 10:  minimal 'rtparseradapter' do
 11>>   libj [
 12:      '',
 13:      '',
 14:      ''
 15:    ]
 16:    action :install
 17:    notifies :restart, 'etdproject[proxy_tle]', :immediately
 18:  end
 20:  etdproject 'proxy_tle' do


Change the name of your variable to avoid the reported conflict, Why is this hard?.


I know how to fix t his specific issue, but it took me two days to find the reason for the error message. It is not very explaining.

I am looking for a general solution to avoid such problems in the future. I don’t know which resources might be introduced in the future.


What you’re seeing looks basically like a “scope” issue. What you are using as a very local hash happens to have the same name as a hash used elsewhere, and I suspect you selected it because it was very short and easy to type. So keeping your hash names more distinctive can be good practice, to help associate them with your specific recipe. I’d use “etd_libj” rather than “libj”, for example, to help segregate it from other cookbooks or system components.