Jason Thompson
Jason Thompson

Reputation: 4833

Is node.run_state in chef supposed to survive the entire chef-client run?

I have a custom resource. When that resource converges, I want to stash the packages installed by said resource into node.run_state[:installed_packages]. But when I go to read the value later on in another recipe (same client run), it appears to be nil.

My only thought is that that is evaluated at resource declaration time? If so, how would I accomplish what I'm intending?

Here's a snippet of my custom resource putting the item into the run_state:

    # Push the desired package into the run_state
    if (node.run_state[:installed_packages] == nil) then
        node.run_state[:installed_packages] = Set.new
    end
    node.run_state[:installed_packages].add("/tmp/#{rpm_name}.rpm")

And here's the template resource I'm using these values in:

template '/tmp/my_script.sh' do
    source 'my_script.sh.erb'
    owner 'root'
    group 'root'
    mode '0755'
    variables({
        :packages => node.run_state[:installed_packages] || Array.new,
    })

    action :nothing
end

Upvotes: 2

Views: 4981

Answers (1)

coderanger
coderanger

Reputation: 54267

It's hard to tell from just what you show but what you think is "later" is probably "before". Use a lazy {} helper to delay evaluation of the value until later. See https://coderanger.net/two-pass/ for more details on when particular bits of code happen.

Upvotes: 3

Related Questions