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

How to get a linux command output to chef attribute

我想将命令输出到 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

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

您的问题的答案在 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 的内容替换为您想要设置的属性。