Chef:Install windows_feature 带有保护子句 not_if
Chef:Install windows_feature with guard clause not_if
我正在使用 Chef 12.4 并在本地模式下使用 chef-client。我正在尝试安装 windows 功能并在安装或删除之前检测它们的当前状态。使用 not if 的原因是因为在使用 DISM 时需要永远检查当前状态。
目前我有一个服务器需要 45 分钟来构建,然后 运行 厨师需要 20 分钟,即使在无事可做的情况下也是如此。
我使用了以下 Powershell 脚本 'service-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
我正在使用的食谱通过查询 dism 的当前功能及其状态,将 'features.txt' 文件写入 Chef 运行 所在的位置。菜谱文件如下:
#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 {} 部分尝试了许多不同的格式,但是其中 none 行得通。我使用了此处的示例:
http://pdalinis.blogspot.co.uk/2013/09/chef-using-powershell-as-conditional.html
我得到的输出是:
* 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
我是 Chef、Ruby 和 Powershell 的新手。我假设 only_if 或 not_if 期望结果为真或假,但这似乎附加了我已添加检查的书面状态(在此脚本中,禁用)。
我检查了powershell脚本,它确实根据状态输出Enabed / Disabled。
我的问题是:
是否可以以这种方式使用 windows_feature 和 powershell_out,根据使用 powershell_script 的示例,但我宁愿使用 windows_feature 安装功能的规范。
如果可以的话,我做错了什么?我感觉它可能是脚本的输出,因为它似乎插入了一个 space:
“启用”
“已禁用”
如果不行,还有其他方法吗?
如果有任何帮助,我将不胜感激。
谢谢。
是否可以只引用 "Disabled"
?否则 Ruby 试图弄清楚它是什么,但不知道任何变量或具有该名称的 class。
我正在使用 Chef 12.4 并在本地模式下使用 chef-client。我正在尝试安装 windows 功能并在安装或删除之前检测它们的当前状态。使用 not if 的原因是因为在使用 DISM 时需要永远检查当前状态。
目前我有一个服务器需要 45 分钟来构建,然后 运行 厨师需要 20 分钟,即使在无事可做的情况下也是如此。
我使用了以下 Powershell 脚本 'service-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
我正在使用的食谱通过查询 dism 的当前功能及其状态,将 'features.txt' 文件写入 Chef 运行 所在的位置。菜谱文件如下:
#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 {} 部分尝试了许多不同的格式,但是其中 none 行得通。我使用了此处的示例: http://pdalinis.blogspot.co.uk/2013/09/chef-using-powershell-as-conditional.html
我得到的输出是:
* 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
我是 Chef、Ruby 和 Powershell 的新手。我假设 only_if 或 not_if 期望结果为真或假,但这似乎附加了我已添加检查的书面状态(在此脚本中,禁用)。
我检查了powershell脚本,它确实根据状态输出Enabed / Disabled。
我的问题是:
是否可以以这种方式使用 windows_feature 和 powershell_out,根据使用 powershell_script 的示例,但我宁愿使用 windows_feature 安装功能的规范。
如果可以的话,我做错了什么?我感觉它可能是脚本的输出,因为它似乎插入了一个 space: “启用” “已禁用”
如果不行,还有其他方法吗?
如果有任何帮助,我将不胜感激。 谢谢。
是否可以只引用 "Disabled"
?否则 Ruby 试图弄清楚它是什么,但不知道任何变量或具有该名称的 class。