user555303
user555303

Reputation: 1374

Questions about Chef cookbook/recipes processing and reboot behavior

I am unclear about, but want to understand, the way that Chef processes examples like the one from here:

https://docs.chef.io/resource_reboot.html

reboot 'Restart Computer' do
  action :nothing
end

powershell_script 'Rename and Join Domain' do
  code <<-EOH
    ...your rename and domain join logic here...
  EOH
  not_if <<-EOH
    $ComputerSystem = gwmi win32_computersystem
    ($ComputerSystem.Name -like '#{node['some_attribute_that_has_the_new_name']}') -and
      $ComputerSystem.partofdomain)
  EOH
  notifies :reboot_now, 'reboot[Restart Computer]', :immediately
end

?

What I mean is: If I have a recipe like the above in a run list that is assigned to a node, does that recipe get "executed" over and over by Chef-client but because of the not_if, it only does the "rename and domain join" part ONCE, but even then the recipe keeps getting run over and over, forever?

Also, and I hope that it is ok to ask this at the same time, if I have several recipes in run list (e.g., in a role), say the recipes are recipe A, B, C, and D, and say, recipe B causes a reboot at the end of the recipe, what happens with recipes C and D?

Should all of the recipes have "guards", so they will all get re-run every time, and if A and B made the changes they were supposed to make, then the next time, they'll skip their processing because of the guards, but then C (and then D) will be processed (this is all assuming that there's a Chef-client service running on the node)?

Thanks, Jim

Upvotes: 0

Views: 384

Answers (1)

Roland
Roland

Reputation: 1426

:immediately notifications will be called, after the current resource ran. If you add a not_if/only_if conditional, the notification will only be executed if the resource ran. Skipped resources won't trigger notifications.

The action :reboot_now of Chef 12+ reboot resource will exactly do, what its name says. You'll have to re-run the chef-client after the reboot and re-start converge.

Use :request_reboot if you want to trigger a reboot at the end of the chef run and when have multiple places which have to pull this trigger.

As you don't want to reboot your system on a regular basis, you almost always want to set guards for the reboots or actions that send reboot notifications.

Upvotes: 1

Related Questions