在Chef配方中,我需要在执行某些操作后在Node上执行重启, 重新启动完成后,我需要继续执行其他操作:
配方: - 行动1 - 动作2 -重启 -action3 -action4 ....
我已经查看了社区中现有的一些食谱:reboot-handler,chef-reboot,chef-restart,chef-dominous,但是我不能让它们工作。
Chef中有一些方法可以满足我的需求吗? 请提供详细的例子。
非常感谢你的帮助。
答案 0 :(得分:1)
如何使用chef tags来实现一系列状态转换?这种方法可以与reboot-handler食谱相结合,以管理实际的重启。
#
# 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
if tagged?("doAction1")
..
..
end
include_recipe "reboot-handler"
if tagged?("doAction2")
..
..
# Trigger reboot at end of chef run
node.run_state['reboot'] = true
end
答案 1 :(得分: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
答案 2 :(得分:0)
我使用FLAG文件方法完成了它,下面给出了示例。 域加入示例 -
块引用
---------------------------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------------------------