Chef Reboot, how to reboot only when required

How do I detect if a reboot is required? One of my recipes is failing because a previous recipe puts the machine in a state that requires a reboot.

I’ve looked through this…

And it shows how to reboot immediately, but won’t that mean the machine reboots everytime chef runs? How do you only reboot if it’s required?

You should define your reboot resource with an action :nothing and trigger it from another resource with the notify block.

You can see examples in the reboot resource page

On the windows side, I do the following

reboot ‘Only if needed’ do
action :request_reboot
reason 'Need to reboot when the run completes successfully.'
delay_mins 2
only_if { reboot_pending? }