How to start chef-client from same state where it left it before server reboot?

Greetings Masters

I am installing certain packages on my server using chef. These installations need server reboot. And we need to perform some post activities once the server comes online.

I want to handle this situation by creating a role.

  • role = recipe[pre_installation_actions], recipe[install_package_and_reboot], recipe[post_installation_actions]

But once the server is rebooted, chef client shuts down. How can I invoke chef-client from the same state again once the server is rebooted?

The simplest approach may be to keep this all together in a single recipie. The package resources should be idempotent so having them run again after the reboot should not cause them to converge if things are configured correctly. If the reboot is needed due to a pending reboot state you can add only_if { reboot_pending? } to the reboot resource or if you are rebooting for another reason, add a different guard specific to your use case.

If you are running chef as a service or scheduled task, then the client should engage after the reboot and rerun your recipie. Again, with the idempotent package resources and a propper guard on the reboot, the recipie will run from the beginning but “effectively” run where it previously rebooted.

We do just this in the ad-join cookbook.

https://github.com/NetDocuments/ad-join-cookbook/blob/master/resources/domain_join.rb

The way we handle it is for the recipe to leave a scheduled task that runs chef on boot. At the end of the chef run the scheduled task is cleaned up.
All the resources that don’t need to be run the second time are skipped due to the only_if statements.

Note that per RFC 062 the way mid chef run reboots are handled is changing.