【问题标题】:Chef Cookbook that starts with a small powershell script, need to pass the returned integer value to Chef以小型 powershell 脚本开头的 Chef Cookbook,需要将返回的整数值传递给 Chef
【发布时间】:2016-06-05 23:22:43
【问题描述】:

我正在尝试完成某人的食谱,我需要能够让 Chef 从此脚本中提取返回的整数 0(一些细节已被删除)关于 Chef 中可能起作用的任何建议?我已经在 Chef 中添加了 Powershell 模块,特别是我在询问 Chef 解析这些数据的顺序 - 如在 action: item 中将此变量交给...谢谢。

include_recipe "chef_handler

powershell_script do
  code <<-EOH
  ###############
  # 
  # Description:
  # Returns 1 if any share or share path allow read/write by the 'Everyone' group (fail)
  # Returns 0 if this condition is not found (pass)


  Try
  {
    #get all shares
    $shares = e | Select-Object Name,Path
    if($shares)
    {
      Foreach ($share in $shares)
      {
        #check everyone permissions
        $shareAccounts = -Name $share.Name
        Foreach ($account in $shareAccounts)
        {
          If ($account.AName -eq 'Everyone')
          {
            return 1
          }
        }

        #check 
        $volumePerm = Get-ACL $share.Path
        if ($volume.Access.Where({$Reference -eq 'Everyone'}))
        {
          return 1
        }
      }
    }
    else
    return 0


    #loop through each share checking for 'Everyone'
    # pass the return values send to Chef or something else.
  }

  {
    #build error message
  }
  Finally
  {
    #return final message
  }
  EOH

  fail "Instance has failed the OpenShares check" if code == 1

end

【问题讨论】:

  • 我们使用的解决方法是将其写入 tmp 文件,从 Chef 读取并删除它。
  • 谢谢Wai Yan,有sn-p 可以分享吗?我没有想到这一点。 :-)

标签: powershell chef-infra cookbook


【解决方案1】:

厨师资源没有输出值。如果 PowerShell 脚本失败,它将中止 Chef 运行,否则将继续。

【讨论】:

  • 感谢 coderanger,这证实了它失败的原因。这就是为什么我更喜欢从自己的混乱开始。
【解决方案2】:

您还可以查看powershell_script 块的returns 参数。如果您指定像[0,1] 这样的值,脚本将不会失败。您也可以不指定任何内容并尝试使用 Ruby rescue 捕获异常。

也就是说,如果您确实需要该值,我的建议是将值写入文件并在过程结束时将其读回,而不是返回它。

您可以参考article 使用 powershell 将内容写入文件,并参考this 使用 Ruby 将文件读取到 String

【讨论】:

  • 很有意思,非常感谢Wai Yan,非常感谢。在开始时,单词 code 创建一个变量,该变量将包含返回的任何内容。它没有被定义为某种类型。所以我会仔细查看这两个链接,我认为 ruby​​ catch 可能是我最感兴趣的链接。现在说还为时过早。然而,你让我看到了一些不同的可能性。现在我需要更好地了解如何在代码中使用救援才能做出决定。我已经迟到了,所以我只会做必须做的事情。谢谢 - 如果我能弄清楚,我会发布更新。:-)
猜你喜欢
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多