Can any one help us to Continue the chef recipe execution even after server reboot.
We have a recipe which will call around 5 recipes, in those recipe 1 requires reboot(Which perform domain join on Windows server). Once server is added to domain, we need to execute remaining 4 recipes which are dependent on domain join.
Can you please let us know how to run continuation of recipe execution once the server got restarted and came back to online,
create a recipe that will execute chef-client -d 300 service to run every 5 minutes and another recipe that will change it once the machine joins the domain.
I had faced the similar situation in past.
In my scenario, there were 5 recipes, and after 3rd recipe we want to
reboot the server and after that chef should execute the remaining two
recipes. this is how I handled this use case
I split the recipes into two sets, first set will contain 3 recipes and
second one will have 2 recipes. third recipe where reboot in called,
before that I created a scheduled task (in windows), that scheduled task
will be called only once and will execute the remaining second set of
recipes. once the last recipe is executed it will delete the scheduled task.
We had similar issue, Hostname change needs reboot, AD Join needs reboot, GPO for assigning WSUS server doesnât apply till after reboot and is needed before running patches.
What we ended up doing is creating a scheduled task to run chef-client on system startup. If a server has been down for a while we want to make sure it converges right away before it starts handling production work loads anyway. This is seperate from the recurring scheduled task that may take a bit after server boot to run. Then we simply added our run list to the role cookbook. The resources that need a reboot we have set to :delayed so it reboots at the end of the chef client run, and the resources that need to wait till after the reboot we have wrapped in an âunlessâ statement checking on âreboot_pending?â
knifing a new server then we end up with 4 reboots before itâs finally ready for use, hostname gets set, chef-client on startup task gets created as well as the recurring scheduled task. Adjoin, and patches donât run. Second server run chef kicks off right away and joins the domain with new host name, patches still donât run. Third reboot GPOâs from domain are applied and in house WSUS server is set and chef server kicks off again and applies patches this time and reboots if any patches require it.
There is no easy way for chef to resume a client run where it left off after reboot. Youâd have to create a system that saves the state of the chef client run and sets an autorun on next boot to kick off and read in the state and continue. Not an easy task. Otherwise building resources with good idempotency and logic you should be able to run every cookbook every time chef runs on a node to verify all resources have successfully converged.
$systemDomain = (Get-WmiObject Win32_ComputerSystem).Domain
If (($systemDomain) -eq âdomain_nameâ)
{
Try {
write-host "$env:computerName is DOMAIN MEMBER"
Remove-Item C:\DomainJoinFlag.txt -ErrorAction Stop
Unregister-ScheduledTask -TaskName âChef client schedule_DJâ -Confirm:$false
} # end of Try
Catch [System.Management.Automation.ItemNotFoundException]
{ write-host "Server is already domain member, Or Exception raised due to either missing FLAG file or Server startup schedule task configuration."
eventcreate /t INFORMATION /ID 0909 /L APPLICATION /SO âChefClient_$env:computerNameâ /D âServer is already domain member, Or Exception raised due to either missing FLAG file or Server startup schedule task configuration. Refer to the CHEF reciepie for DomainJoin or check Administrative credentials for creting schedule taskâ}
}
else { write-host "$env:computerName is NOT domain member, joining the server to the domain. Server will be rebooting in a whileâŚ"
eventcreate /t INFORMATION /ID 0909 /L APPLICATION /SO âChefClient_$env:computerNameâ /D "Joining the server : $env:ComputerName to the domain ININLAB.COM (Server Time): $currentTimeString"
New-Item C:\DomainJoinFlag.txt -type file -force
write-host â$env:computerName DOMAIN JOIN INITIATED for the serverâ
$cred = New-Object System.Management.Automation.PsCredential(âdomain\domain_userâ, (ConvertTo-SecureString âPasswordâ -AsPlainText -Force))
Add-Computer -DomainName âdomain_nameâ -Credential $cred -OUPath "OU=HyperV,OU=Infrastructure,OU=Servers,DC=domain,DC=name"
shutdown -r -t 120
} #end_of_else
domainJoin
notifies :run, âwindows_task[Chef client schedule_DJ]â, :immediately
end
windows_task âChef client schedule_DJâ do
user 'SYSTEMâ
command 'chef-client -L C:\chef\chef-client_after_reboot_domainJoin.logâ
run_level :highest
frequency :onstart
frequency_modifier 30
action :create
only_if { ::File.exist?(âC:\DomainJoinFlag.txtâ) }
end