【问题标题】:Chef NOT_IF and ONLY_IF validation issue in Windows recipesWindows 食谱中的 Chef NOT_IF 和 ONLY_IF 验证问题
【发布时间】:2014-08-29 04:01:33
【问题描述】:

我正在运行这个简单的配方块来在 IIS 中创建一个 Web 应用程序

powershell_script "create_site_my_site" do
    code "New-webapppool -name 'My_Web_App'; New-Website -Name 'My_Web_App' -applicationpool 'My_Web_App' -Port '80' -IPAddress * -PhysicalPath 'c:\webs\My_Web_App'  "
    action :run
    not_if "get-website | where-object { $_.name -eq  'My_Web_App' }"
end

这里的问题在于 NOT_IF 部分始终为真

PS C:\Users\carlos>
PS C:\Users\carlos> get-website | where-object { $_.name -eq 'asdfasdfasdf' }
PS C:\Users\carlos> echo $lastexitcode
1
PS C:\Users\carlos> get-website | where-object { $_.name -eq 'My_Web_App' }

Name        ID    State    Physical Path       Bindings
----        --    -----    -------------       --------
My_Web_App  6     Stopped  c:\webs\My_Web_App  http *:80:    
                                               https *:443:

PS C:\Users\carlos.camacho> echo $lastexitcode
1

现在,我的问题是关于如何在 NOT_IF 中返回 True 或 False 取决于我的代码返回值??

【问题讨论】:

    标签: ruby powershell chef-infra chef-recipe


    【解决方案1】:
    powershell_script "create_site_my_site" do
        guard_interpreter :powershell_script
        code "New-webapppool -name 'My_Web_App'; New-Website -Name 'My_Web_App' -applicationpool 'My_Web_App' -Port '80' -IPAddress * -PhysicalPath 'c:\webs\My_Web_App'  "
        action :run
        not_if "get-website | where-object { $_.name -eq  'My_Web_App' }"
    end
    

    您需要指定guard_interpreter :powershell_script。请注意CHEF-1943 并确保guard_interpreter 在警卫之前,并且不要将警卫置于块中。

    powershell_script documentation

    关于 Chef powershell 提供程序的一个很好的参考是 this video

    简单地使用IIS cookbook 是另一种选择。

    【讨论】:

    • 我刚刚更新了我的答案。显然,not_if 守卫只有在包含在块中时才会为 powershell 执行!
    • 提出了一个关于 PowerShell 保护解释器作为字符串的问题:请参阅此问题:github.com/opscode/chef/issues/1943
    【解决方案2】:

    您应该利用 windows 食谱中的 powershell_out 资源。

    如果输出为空,您可以检查命令的错误与否。

    在你的情况下应该这样做:

    only_if powershell_out("get-website | where-object { $_.name -eq  'My_Web_App' }").stdout.empty?
    

    【讨论】:

      【解决方案3】:

      我认为这是因为您没有告诉 not_if guard 使用 powershell_script interperter。

      这行得通吗?

      powershell_script "create_site_my_site" do
          code "New-webapppool -name 'My_Web_App'; New-Website -Name 'My_Web_App' -applicationpool 'My_Web_App' -Port '80' -IPAddress * -PhysicalPath 'c:\webs\My_Web_App'  "
          action :run
          not_if "$(get-website | where-object { $_.name -eq  'My_Web_App' }).count -gt 0"
          guard_interpreter :powershell_script
      end
      

      【讨论】:

      • 我不确定,主要问题是命令总是返回 1(根据 OP),这就是为什么我指出 powershell_out 并测试空输出......但再一次,更换警卫解释器就足够了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-24
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多