How to use a Ruby block to assign variables in chef recipe

后端 未结 1 1019
终归单人心
终归单人心 2020-12-16 08:17

I am trying to execute Ruby logic on the fly in a Chef recipe and believe that the best way to achieve this is with a block.

I am having difficulty transferring vari

相关标签:
1条回答
  • 2020-12-16 08:47

    Your problem is compile time versus converge time. Your block will be run at converge time but at this time the node['host'] in the execute resource has already been evaluated.

    The best solution in this case would be to use lazy evaluation so the variable will be expanded when the resource is converged instead of when it is compiled:

    execute "Enable on hosts" do
      command lazy { "#{path}/command --enable -N #{node['hosts']}" }
    end
    

    Here's a little warning though, the lazy operator has to include the full command attribute text and not only the variable, it can work only as first keyword after the attribute name.

    0 讨论(0)
提交回复
热议问题