Chef NOT_IF and ONLY_IF validation issue in Windows recipes

核能气质少年 提交于 2019-12-12 10:04:07

问题


Im runing this simple recipe block to create a web app in IIS

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

The problem here its that the NOT_IF part its allways 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

Now, my question is about how to return True or False in the NOT_IF depending on my code return value ??


回答1:


You should take advantage of the powershell_out resource from the windows cookbook.

With that you may check the error of a command or not ot if the output is empty.

In your case this should do:

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



回答2:


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

You need to specify guard_interpreter :powershell_script. Be aware of CHEF-1943 and make sure guard_interpreter is before the guard and do not put the guard in a block.

powershell_script documentation

An excellent reference on the Chef powershell provider is this video.

Simply using the IIS cookbook is another option.




回答3:


I think this is because you're not telling the not_if guard to use the powershell_script interperter.

Does this work?

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


来源:https://stackoverflow.com/questions/25406353/chef-not-if-and-only-if-validation-issue-in-windows-recipes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!