【问题标题】:Linux Command Output to Chef AttributeLinux 命令输出到 Chef 属性
【发布时间】:2019-01-08 14:28:01
【问题描述】:

问题是我需要将 Linux 命令的值分配给 CHef 属性。但无法做到。

Im using the below code and not finding the result. Kindly Help what im 
missing

ruby_block "something" do
  block do
    Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
    node.default['foo'] = shell_out("echo Hello world").stdout
  end
  action :create
end



log "demo" do
  message lazy { node['foo'] }
end

以下是运行日志:

Starting Chef Client, version 13.9.1
resolving cookbooks for run list: ["sample_repo"]
Synchronizing Cookbooks:
  - sample_repo (0.1.4)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 2 resources
Recipe: sample_repo::default
  * ruby_block[something] action create
    - execute the ruby block something
  * log[demo] action write


Running handlers:
Running handlers complete
Chef Client finished, 2/2 resources updated in 02 seconds

提前致谢

【问题讨论】:

    标签: linux attributes chef-infra devops


    【解决方案1】:

    您的代码很好,日志消息没有显示,因为log 资源的默认级别是:info,默认情况下chef-client 在交互运行时不会显示信息级别的日志消息。也就是说,这种将内容存储在节点属性中的代码非常脆弱,除非特别需要,否则可能不应该使用。最好这样做:

    message lazy { shell_out("echo Hello world").stdout }
    

    此外,您不需要任何时髦的变异include AFAIK 之类的东西,默认情况下,shell_out 助手在大多数情况下都可用。此外,您通常应该使用shell_out!() 而不是shell_out(),如果命令失败,! 版本会自动引发异常。除非您特别希望允许失败的命令,否则请使用 ! 版本。

    【讨论】:

      猜你喜欢
      • 2015-06-25
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2014-02-02
      • 2014-01-14
      • 1970-01-01
      • 2021-12-10
      • 2021-03-29
      相关资源
      最近更新 更多