ruby 如何获得一个 linux 命令输出到厨师属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29721575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get a linux command output to chef attribute
提问by SASI
I want to get a command output into a chef attribute. Can some one help me how to set that in execute resource or bash resource.
我想将命令输出放入厨师属性中。有人可以帮助我如何在执行资源或 bash 资源中设置它。
ruby_block "something" do
block do
#tricky way to load this Chef::Mixin::ShellOut utilities
Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
command = 'cat #{fileName}'
command_out = shell_out(command)
node.set['my_attribute'] = command_out.stdout
end
action :create
end
How to use attributes in the above code..
如何在上面的代码中使用属性..
回答by StephenKing
The answer to your question is pretty mich given in How can I put the output of a Chef 'execute resource' into a variable. With tiny modification, if I understand the question right, your problem can be solved like this:
您的问题的答案在How can I put the output of a Chef 'execute resource' into a variable 中非常详细。稍加修改,如果我理解正确,您的问题可以这样解决:
ruby_block "something" do
block do
#tricky way to load this Chef::Mixin::ShellOut utilities
Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
command = 'cat /etc/hostname'
command_out = shell_out(command)
node.set['my_attribute'] = command_out.stdout
end
action :create
end
Replace the content of commandwith the command that you want to run and my_attributewith the attribute that you want to set.
将 的内容替换为command要运行的命令和my_attribute要设置的属性。

