【问题标题】:Chef:Install windows_feature with guard clause not_ifChef:使用保护子句 not_if 安装 windows_feature
【发布时间】:2015-11-08 11:01:18
【问题描述】:

我正在使用 Chef 12.4 并在本地模式下使用 chef-client。我正在尝试安装 Windows 功能并在安装或删除之前检测它们的当前状态。使用 not if 的原因是因为使用 DISM 时需要永远检查当前状态。

目前我有一个服务器需要 45 分钟构建,然后 20 分钟运行 chef,即使无事可做。

我使用以下 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 {} 段中尝试了许多不同的格式,但都没有奏效。我使用了这里的示例: 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 脚本,它确实根据状态输出启用/禁用。

我的问题是:

  1. 是否可以这样使用windows_feature和powershell_out,根据示例是使用powershell_script,但我宁愿使用windows_feature规范来安装功能。

  2. 如果可能,我做错了什么?我感觉它可能是脚本的输出,因为它似乎插入了一个空格: “启用” “已禁用”

  3. 如果不可能,还有其他方法吗?

如果能提供任何帮助,我将不胜感激。 谢谢。

【问题讨论】:

    标签: ruby powershell chef-infra


    【解决方案1】:

    您是否可能只需要引用"Disabled"?否则 Ruby 会试图弄清楚它是什么,但不知道任何具有该名称的变量或类。

    【讨论】:

    • 太棒了,我需要新鲜的眼光(可能还需要阅读更多关于 ruby​​ 的基础知识!)。
    猜你喜欢
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多