user3540835
user3540835

Reputation: 453

unable to delay the evaluation of code block till run time in chef recipe

I have a chef recipe where I want to include a gem file and use it as below

gem_package "my-gem"
require '<my-gem>'
my_var = <Using methods supported by gem>

However this fails during compilation with error "cannot load such file " Is there any way I can delay this check so that during the run gem gets installed and can be used.

I don't want to use a ruby block (Not sure even if that would work or fail during compilation) , since I am not sure how could I make "my_var" variable available outside the ruby block in other chef-resource or during any conditional checks that are done outside the ruby block.

Upvotes: 0

Views: 443

Answers (1)

coderanger
coderanger

Reputation: 54181

There are two answers here. First is to the question you asked, you could wrap the code in a ruby_block resource. That will delay evaluation until the converge phase.

The real answer to this question is that you should be using a chef_gem resource instead of gem_package. That automatically installs the gem to Chef's ruby installation and does it at compile time to avoid this problem.

Upvotes: 2

Related Questions