Is there any way to delay a resource's attribute resolution until the “execute” phase?

后端 未结 2 1364
一整个雨季
一整个雨季 2021-02-20 02:48

I have two LWRPs. The first deals with creating a disk volume, formatting it, and mounting it on a virtual machine, we\'ll call this resource cloud_volume. The seco

相关标签:
2条回答
  • 2021-02-20 03:30

    A cleaner way would be to use Lazy Attribute Evaluation. This will evaluate node[:volumes][mount_point][:uuid] during execution time instead of compile

    foobar "#{mount_point}" do
      disk_uuid lazy { node[:volumes][mount_point][:uuid] }
      action [:do_stuff]
    end
    
    0 讨论(0)
  • 2021-02-20 03:52

    Disclaimer: this is the way to go with older Chef (<11.6.0), before they added lazy attribute evaluation.

    Wrap your foobar resource into ruby_block and define foobar dynamically. This way after the compile stage you will have a ruby code in resource collection and it will be evaluated in run stage.

    ruby_block "mount #{mount_point} using foobar" do
      block do
        res = Chef::Resource::Foobar.new( mount_point, run_context )
        res.disk_uuid node[:volumes][mount_point][:uuid]
        res.run_action :do_stuff
      end
    end
    

    This way node[:volumes][mount_point][:uuid] will not be known at compile time, but it also will not be accessed at compile time. It will only be accessed in running stage, when it should already be set.

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