【问题标题】:Chef - using powershell_script from within ruby_blockChef - 在 ruby​​_block 中使用 powershell_script
【发布时间】:2017-06-30 03:35:24
【问题描述】:

是否可以在 ruby​​_block 中使用 powershell_script? 我想在收敛阶段而不是编译阶段运行 powershell 脚本。 当前代码不起作用:

ruby_block 'ruby block so that code is run during convergence phase and not compilation phase' do
   block do
      buildNumber = "123"
      powershell_script 'run powershell script' do
      environment ({'buildNumber' => buildNumber})
      code "path/to/script/script.ps1"
      end
   end
action :run
end

我知道您可以在 ruby​​_block 外部的 powershell_script 上使用保护以使其在收敛期间运行,但我需要在 ruby​​ 块内定义的局部变量 buildNumber。

【问题讨论】:

    标签: ruby chef-infra


    【解决方案1】:

    以下代码对我有用:

    x = Chef::Resource::PowershellScript.new('unzipper script',run_context)
        x.code 'D:/git/chef/cookbooks/java-8-upgrade/unzipper.ps1'
        x.environment ({'buildNumber' => buildNumber})
        x.run_action :run
    

    【讨论】:

    • 请不要使用它,它在某些情况下确实无法正常工作(例如 ChefSpec 测试)。
    【解决方案2】:

    在简单的情况下,您可能可以使用powershell_out 辅助方法。对于更复杂的情况,制作自定义资源并使用普通资源。不要使用手动资源调用(Chef::Resource::Foo.new,因为我们明确不支持这一点并且知道它会破坏事情)。

    【讨论】:

    • 嘿,感谢您的回复以及对昨天问题的评论对我帮助很大。你认为你可以提供一个使用 powershell_out 的例子吗?我真的不知道从哪里开始
    • 它的工作方式与shell_out 类似,但您提供的字符串是使用 powershell 而不是 cmd.exe 运行或直接运行。
    猜你喜欢
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-23
    相关资源
    最近更新 更多