我对Chef很新,但我已经完成了所有的在线教程,觉得我对基础知识有了很好的把握。我正在尝试从食谱中运行Powershell脚本。脚本本身从Powershell运行良好,并按预期更新DNS IP设置。当我运行配方它也完成没有错误但嵌入式脚本不更新DNS设置所以我假设它没有正确执行,我不知道在哪里寻找错误/日志。
食谱发布在下方,通过chef-client.bat --local-mode --runlist 'recipe[prod_server::dns_settings]'
powershell_script 'Set DNS Servers' do
code = <<-EOH
$primary = "10.1.10.2"
$secondary = "10.1.10.225"
$DNSServers = "$primary","$secondary"
$message=""
function setDNS($DNSServers)
{
try
{
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration |where{$_.IPEnabled -eq "TRUE"}
Foreach($NIC in $NICs)
{
$message += $NIC.SetDNSServerSearchOrder(@($DNSServers)) | Out-String # set the DNS IPs and capture output to string
}
}
catch
{
$message += "An error occcured while setting NIC object." + "`n`rError: $_";
}
#write-host $message #if necessary, display result messages
}
setDNS($DNSServers)
EOH
end
答案 0 :(得分:1)
我95%确定你在尝试运行脚本并且没有正确处理它时确实遇到了错误。我知道,因为这个确切的错误恰好发生在我身上,一个沉默的失败,对我而言,这是因为双引号。
两个解决方法:
powershell_script 'Set DNS Servers' do
code = <<-EOH
$primary = '10.1.10.2'
$secondary = '10.1.10.225'
$DNSServers = $primary,$secondary
$message=''
function setDNS($DNSServers)
{
try
{
$NICs = Get-WMIObject Win32_NetworkAdapterConfiguration |where{$_.IPEnabled -eq 'TRUE'}
Foreach($NIC in $NICs)
{
$message += $NIC.SetDNSServerSearchOrder(@($DNSServers)) | Out-String # set the DNS IPs and capture output to string
}
}
catch
{
$message += 'An error occcured while setting NIC object.' + '`n`rError: $_';
}
#write-host $message #if necessary, display result messages
}
setDNS($DNSServers)
EOH
end
```
script_path =
win_friendly_path(File.join(Chef::Config[:file_cache_path],"os-permissions.ps1"))
script_name = "os-permissions.ps1"
execute script_name do
command "powershell \"#{script_path}\""
action :nothing
end
cookbook_file script_path do
source "os-permissions.ps1"
action :create
notifies :run, "execute[#{script_name}]", :immediately
end
#2,我的食谱中有一个脚本,名为os-permissions.ps1,在files \ default \
中当我无法从脚本中删除所有双引号时,我使用#2。我有时也会直接从cookbook直接执行脚本,但这并不是那么简单,也可能不是最好的练习(只是我很懒)