如何将linux命令输出到chef属性

时间:2015-04-18 19:05:30

标签: ruby chef recipe

我想将命令输出转换为chef属性。有人可以帮助我在执行资源或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

如何在上面的代码中使用属性..

1 个答案:

答案 0 :(得分:18)

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

使用您要运行的命令替换command的内容,并使用您要设置的属性替换my_attribute