2015-11-08 48 views
0

我使用廚師12.4和本地模式下使用廚師客戶端。我試圖在安裝或刪除之前安裝Windows功能並檢測其當前狀態。使用not if的原因是因爲在使用DISM時需要檢查當前狀態。廚師12.4 windows_feature與not_if

目前我有一臺服務器,需要45分鐘的時間才能建立,然後20分鐘即可在無所事事的情況下運行廚師。

我用下面的PowerShell腳本「服務check.ps1以輸出包的狀態,這是由配方本身到位:

$content = Get-Content .\features.txt 
$feature = $args[0] 

function grep($f) { 
    foreach($_ in $content){ 
    $data = $_.split("|") 
    %{if($($data[0]) -match $f) {return "$($data[1])"}} 
    } 
} 

grep $feature 

我使用的配方寫'features.txt'文件添加到廚師正在運行的位置,方法是查詢當前功能及其狀態。配方文件如下:

#Check the features and their current state 
powershell_script 'installed_features' do 
    code <<-EOH 
    dism /online /get-features /format:table > c://chef//features.txt 
    EOH 
end 

cookbook_file 'C:\Scripts\service-check.ps1' do 
    source 'service-check.ps1' 
end 

windows_feature 'TelnetClient' do 
    action :remove 
    only_if { 
    status = powershell_out("Powershell -f C:\\scripts\\service-check.ps1 TelnetClient").stdout.chop 
    Disabled != status 
    } 
end 

我已經在only_if {}段中嘗試了許多不同的格式,但是他們都沒有工作。我使用的是這裏的例子:我在廚師,紅寶石和PowerShell新手

* windows_feature[TelnetClient] action remove 

================================================================================ 
Error executing action `remove` on resource 'windows_feature[TelnetClient]' 
================================================================================ 

NameError 
--------- 
uninitialized constant Chef::Recipe::Disabled 

Resource Declaration: 
--------------------- 
# In C:/chef/local-mode-cache/cache/cookbooks/tester/recipes/default.rb 

12: windows_feature 'TelnetClient' do 
13: action :remove 
14: only_if { 
15:  status = powershell_out("Powershell -f C:\\scripts\\service-check.ps1 TelnetClient").stdout.chop 
16:  Disabled != status 
17: } 
18: end 

Compiled Resource: 
------------------ 
# Declared in C:/chef/local-mode-cache/cache/cookbooks/tester/recipes/default.rb:12:in `from_file' 

windows_feature("TelnetClient") do 
    provider LWRP provider windows_feature_dism from cookbook windows 
    action [:remove] 
    retries 0 
    retry_delay 2 
    default_guard_interpreter :default 
    declared_type :windows_feature 
    cookbook_name "tester" 
    recipe_name "default" 
    only_if { #code block } 
end 

http://pdalinis.blogspot.co.uk/2013/09/chef-using-powershell-as-conditional.html

我得到的輸出。我假設only_if或not_if期望得到真或假的結果,但是這似乎附加了我已經添加了檢查的書面狀態(在此腳本中禁用)。

我檢查了powershell腳本,它確實根據狀態輸出Enabed/Disabled。

我的問題是:

  1. 是否有可能使用windows_feature和powershell_out以這種方式,根據它使用powershell_script的例子,但我寧願使用windows_feature規範安裝的特點。

  2. 如果有可能,我做錯了什麼?我有一種感覺,因爲它似乎插入空格可能是腳本的輸出: 「已啓用」 「已禁用」

  3. 如果這是不可能,有另一種方式做到這一點?

我非常感謝您的幫助。 謝謝。

回答

1

是否有可能您只需要報價"Disabled"?否則,Ruby正試圖弄清楚它是什麼,但不知道任何具有該名稱的變量或類。

+0

太棒了,我需要新鮮的眼睛(並且可能會閱讀更多關於紅寶石的基礎知識!)。 –