2

I'm trying to use directory resource under Chef LWRP, but unable to access the attribute inside the resource block, is there a way to do it. Or am I totally wrong and need different approach to achieve this.

my-cookbook/providers/default.rb

use_inline_resources

action :setup do
  directory node["#{@new_resource.name}"] do
    action :create
    not_if {node["#{@new_resource.name}"].include? "test"}
  end
end

The @new_resource.name on the line not_if {node["#{@new_resource.name}"].include? "test"} is evaluated as nilClass,

while it gets properly evaluated in directory node["#{@new_resource.name}"] do line

Thanks

Shan
  • 2,141
  • 2
  • 17
  • 32

1 Answers1

1

At last, found it

When referring to variable inside another resource, we need to access it without @.

action :setup do
  new_resource = @new_resource
  directory node["#{@new_resource.name}"] do
    action :create
    not_if {node["#{new_resource.name}"].include? "test"}
  end
end

Thanks to @stajkowski (GitHub)

osexp2000
  • 2,910
  • 30
  • 29
Shan
  • 2,141
  • 2
  • 17
  • 32
  • And you should not use interpolation when you just call a variable neither. Btw, You code sounds really strange here and there's probably another approach to what you're after... – Tensibai Dec 12 '16 at 10:05
  • Hi @Tensibai. I hope you are right, there would be a better solution. The use case is like, I have a recipe, which needs to be called with different attributes every time, and as chef does not call include_recipe more than once, I'm just making the recipe an LWRP (a copy paste of the recipe actually). Its a long recipe, I passed only one resource to explain my problem. Thank you so much. – Shan Dec 13 '16 at 00:39