Chef - how to restart the virtual machine and continue to perform actions

In the chef’s recipe, I need to reboot to Node after doing some things, and after the reboot is complete, I need to continue with the following steps:

Recipe: -action 1 -action 2 -reset -action3 -action4 ....

I checked some existing cookbooks in the community: reboot-handler, chef-reboot, chef-restart, chef-dominous, but I can't get them to work.

Is there any method in the Chef to get what I need? Please provide detailed examples.

Thank you very much for your help.

+4
source share
3 answers

- ? reboot-handler cookbook .

default.rb

#
# Action recipes
#
include_recipe "mycookbook::action1"
include_recipe "mycookbook::action2"

#
# State transitions
#
if tagged?("doAction1")

  untag("doAction1")
  tag("doAction2")

elsif tagged?("doAction2")

  untag("doAction2")

end    

action1.rb

if tagged?("doAction1")

  ..
  ..

end

action2.rb

include_recipe "reboot-handler"

if tagged?("doAction2")

  ..
  ..

  # Trigger reboot at end of chef run
  node.run_state['reboot'] = true
end
+1
This is a recipe of mine you can try.
# Cookbook Name:: reboot-test
# Recipe:: default

bash 'reboot' do
     cwd '/tmp'
      code <<-EOH
      touch reboot.lock
      chmod +x /etc/rc.d/rc.local
#     echo "/usr/local/bin/chef-client" >> /etc/rc.d/rc.local
      cp /etc/rc.d/rc.local /etc/rc.d/rc.local_bkp
      echo "/bin/chef-client" >> /etc/rc.d/rc.local
      reboot
      EOH
      ignore_failure true
      not_if do ::File.exists?('/tmp/reboot.lock') end
      not_if 'ls -lrth /tmp/reboot.lock'
     end

#This is where you need to code for the config automation like the execute #resource but before the bash resource "CHEF-CLIENT-#EXIT"

execute 'touch' do
     command '/bin/touch /tmp/suman.test'
     end

bash 'chef_client_exit' do
      code <<-EOH
      rm -rf /tmp/reboot.lock
#     sed -i '/\/usr\/local\/bin\/chef-client/d' /etc/rc.d/rc.local
#     sed -e '/^\/bin\/chef-client/d' -i /etc/rc.d/rc.local
#     sed -e '\|^/bin/chef-client|d' -i /etc/rc.d/rc.local
      cp /etc/rc.d/rc.local_bkp /etc/rc.d/rc.local
      rm -rf /etc/rc.d/rc.local_bkp
      EOH
      only_if 'grep -i chef-client /etc/rc.d/rc.local'
     end
+1

FLAG , , . -

Blockquote

---------------------------Script Start------------------------

    powershell_script 'Domain_Join' do
    guard_interpreter :powershell_script
    code -domainJoin
    $currentTime = Get-Date
    $currentTimeString = $currentTime.ToString()

  $systemDomain = (Get-WmiObject Win32_ComputerSystem).Domain
  If (($systemDomain) -eq 'domainName')
  { 
    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 "domainName" -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

---------------------------Script Ends------------------------
0

Source: https://habr.com/ru/post/1546615/


All Articles