0
votes

How do I set the node level attributes in ruby block. I have a use case where I use the attribute across all the resources. I was reading about converge vs compile time, I get it. But I need some suggestions on how to use the lazy evaluation here.

Here is my code, reads a json file and use the value as a node attribute

ruby_block 'package' do
  block do
    file = open("/tmp/pkg.json")
    json = file.read
    parsed = JSON.parse(json)
    node.override['artifact']['version'] = parsed["artifact"]["version"]
  end
  only_if { File.exist?("/tmp/pkg.json") }
  action :run
end

and recipe follows as

deploy_art = "#{id}-#{node['artifact']['version']}.war". <<--- How do I use lazy eval here?

Tried this, no luck

deploy_art = "#{id}-lazy{ #{node['artifact']['version']}}.war" with lazy eval

I would like to construct deploy_art variable with ruby_block attribute for further logic(s) to work across the recipe. Apparently, I would like to use lazy eval as a clean way.

1

1 Answers

0
votes

It has to be around the whole property, whatever the property is. So if you had:

some_resource 'deploythething' do
  artifact lazy { "#{id}-#{node['artifact']['version']}.war" }
end

that would work. You can do deploy_art = lazy { "#{id}-#{node['artifact']['version']}.war" } but remember that will only work if deploy_art is used as the value for a property so that it's effectively the same as the above example.