ruby - Windows Recipe 中的 Chef NOT_IF 和 ONLY_IF 验证问题

标签 ruby powershell chef-infra chef-recipe

我正在运行这个简单的配方 block 以在 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 部分总是 True

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 取决于我的代码返回值??

最佳答案

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是另一种选择。

关于ruby - Windows Recipe 中的 Chef NOT_IF 和 ONLY_IF 验证问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25406353/

相关文章:

mysql - 延迟 Chef 直到资源完成其任务

ruby 调试 : print line number when taking too long

javascript - Rails, "respond_to do |format|"返回 ActionController::UnknownFormat

ruby-on-rails - 如何将变量传递到此部分以用作计数器?

powershell - Windows Server 2012R2 Core - 无法运行 powershell DSC

chef-infra - Chef 从模板和 Knife 搜索中创建逗号分隔的字符串

ruby-on-rails - 如何从字符串转换为对象属性名称?

powershell - 无法启动 Windows 文件夹外的程序

c - 在 Windows PowerShell 中为 GCC 设置别名

ubuntu - 如何使用 Chef 在 crontab 中设置环境变量?