Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.5k views
Welcome To Ask or Share your Answers For Others

1 Answer

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:

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 command with the command that you want to run and my_attribute with the attribute that you want to set.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...