Fabio Cecaro
Fabio Cecaro

Reputation: 3

Chef: Process sequence in a recipe

my question is to verify the processes in my cookbook.

My test is with Chef 12.18.31 Ubuntu 16.04 - AWS EC2 - Opsworks Stack

So, if I have a recipe with a sequence like this:

Native_Chef_Resource do
Chef::Log.info("First")
My_First_Variable
end

My_Command do
my script based on My_First_Variable
end

My_Resource do
Chef::Log.info("Second")
My_Second_Variable
end

My_Other_Command do
my script based on My_Second_Variable
end

I noticed that the native chef resource is executed in time to have value into My_Command, but the resources that I create into my cookbook don't respect ever the sequence and I don't have the value in My_Other_Command.

After this doubt, I tryied "Notifies" with these recipe and resource.

Recipe

test 'test notify' do
par_test 'primo test'
notifies :run, 'execute[bo]', :immediately
end

execute 'bo' do
command 'echo uname -a'
Chef::Log.info("execute bo")
end

Resource

resource_name :test
property :par_test
default_action :create
action :create do
Chef::Log.info("#{par_test}")
end

I made two test, one with 'immediately' and other with 'before'.

The results:

Test with immediately [2017-10-15T12:54:41+02:00] INFO: execute bo [2017-10-15T12:54:41+02:00] INFO: Processing test[test notify] action create (deploy::test_notify line 1) [2017-10-15T12:54:41+02:00] INFO: primo test [2017-10-15T12:54:41+02:00] INFO: Processing execute[bo] action run (deploy::test_notify line 6) [2017-10-15T12:54:41+02:00] INFO: execute[bo] ran successfully

Test with before

[2017-10-15T13:16:31+02:00] INFO: execute bo [2017-10-15T13:16:31+02:00] INFO: test[test notify] running why-run create action to support before action [2017-10-15T13:16:31+02:00] INFO: Processing test[test notify] action create (deploy::test_notify line 1) [2017-10-15T13:16:31+02:00] INFO: primo test [2017-10-15T13:16:31+02:00] INFO: Processing test[test notify] action create (deploy::test_notify line 1) [2017-10-15T13:16:31+02:00] INFO: primo test [2017-10-15T13:16:31+02:00] INFO: Processing execute[bo] action run (deploy::test_notify line 6) [2017-10-15T13:16:31+02:00] INFO: execute[bo] ran successfully

What do you think about sequence of executions? Thanks

Upvotes: 0

Views: 59

Answers (1)

coderanger
coderanger

Reputation: 54249

I've got a longer description of this at https://coderanger.net/two-pass/ but basically:

Any Ruby code in a recipe at the base level (which includes all the calls to create resources) is run when the recipe file is compiled. This includes any code in in the main do ... end block of the resource (as opposed to the block property of ruby_block or anything inside lazy{}) as that block is instance_eval'd during the creation of the resource object. The action method for a resource is run during the converge phase, after all recipes in the run list are compiled. This process is repeated in a recursive way for custom resources, who's action methods are their own mini compile and converge phases.

Upvotes: 0

Related Questions