【问题标题】:Chef / InSpec powershell command result within Test测试中的 Chef / InSpec powershell 命令结果
【发布时间】:2018-03-30 18:34:21
【问题描述】:

背景 我写了一本安装 Windows 功能的食谱。某些功能依赖于父功能。父功能可能没有安装该功能所需的源文件。

在我的配方中,我使用 only_if 调用 Powershell 命令来确定源文件是否存在。

(Get-WindowsFeature | Where Name -eq NET-Framework-Core | Select InstallState).InstallState -eq 'Removed'

如果安装状态等于已删除,则从属功能没有所需的源文件,并且如果不提供它们就无法安装。因此,如果我的说明书确定源文件丢失,它不会尝试安装这些功能。但是,如果源文件确实存在,则说明书将安装这些功能。这部分运行良好。

问题 我有 InSpec 测试来验证是否安装了正确的 Windows 功能。我想使用 Powershell 命令的结果运行或跳过特定测试。我想不出一种方法来调用上面的 Powershell 命令、获取结果并在 InSpec 中运行或跳过测试。

【问题讨论】:

    标签: powershell chef-infra inspec


    【解决方案1】:

    有两个主要选项。一种是复制逻辑以检查源文件是否存在于您的 InSpec 代码中(粗略)。另一种是写出一个令牌文件(即只需触摸该文件),如果不进行安装并在 InSpec 中使用file 资源检查它。

    【讨论】:

      【解决方案2】:

      经过一番挖掘,我发现了这个InSpec issue on git hub

      他们添加了在 InSpec 中使用 only_if 的功能(我不知道)。我使用 powershell 资源调用我的 powershell 命令,将标准输出转换为布尔值并返回它。我将提供我想出的粗略代码以供参考。我是 ruby​​ 新手,所以我确信有更好的方法来编写代码。

      control 'Recipe windows_features.rb .NET 3.5 Features' do
        impact 1.0
        title 'Required .NET 3.5 Windows Features Are Installed'
        only_if do
          powershell_command_script = <<-EOH
          (Get-WindowsFeature | Where Name -eq NET-Framework-Core | Select InstallState).InstallState -ne 'Removed'
          EOH
          command_result = powershell(powershell_command_script)
          case command_result.stdout
          when true, "True\r\n" then true
          when false, "False\r\n" then false
          else
            raise ArgumentError, "invalid value: #{command_result.stdout.inspect}"
          end
      
        end
        describe windows_feature('WAS-NET-Environment') do
          it { should be_installed }
        end
        describe windows_feature('Web-Asp-Net') do
          it { should be_installed }
        end
        describe windows_feature('Web-Net-Ext') do
          it { should be_installed }
        end
        describe windows_feature('Web-Mgmt-Console') do
          it { should be_installed }
        end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-28
        • 2016-12-09
        • 1970-01-01
        • 2016-11-02
        • 1970-01-01
        • 2013-12-30
        相关资源
        最近更新 更多