How to get a linux command output to chef attribute

28,708

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.

Share:
28,708

Related videos on Youtube

SASI
Author by

SASI

Updated on July 09, 2022

Comments

  • SASI
    SASI almost 2 years

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

    • dax
      dax about 9 years
      please add some code so we can see the problem you're facing
    • akrishnamo
      akrishnamo about 9 years
      Seems like a question for serverfault.com, yet still not clear what you want.
    • scones
      scones almost 8 years
      The actual problem seems to be command = 'cat #{fileName}' where @SASI should have used double quotes to enable parsing (ie. command = "cat #{fileName}")
  • StephenKing
    StephenKing about 9 years
    This IMHO at least answers your question. If you describe your problem, we might find a way more elegant solution.
  • SASI
    SASI about 9 years
    Thanks StephenKing.. my command is "sudo su - grid +ASM asmcmd lsdsk -G data" I have to login as grid user and enter into +ASM process and run the command. Can you please help me how to run that in recipe.
  • Tensibai
    Tensibai about 9 years
    @user3886612 Edit your question to describe the use case, so we can cast a re-open vote
  • Tensibai
    Tensibai about 9 years
    @SASI repeating yourself in comments won't help. there's an edit button under your question to update it.
  • StephenKing
    StephenKing about 9 years
    It would be also interesting to see, in what way you have to use that information later.
  • Elan Hasson
    Elan Hasson about 7 years
    What does Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut) do in this?
  • StephenKing
    StephenKing about 7 years
    This makes the functions of Chef::Mixin::ShellOut available within the ruby_block.
  • Deepak Deore
    Deepak Deore almost 6 years
    There is deprecation warning for using node.set, that is being replaced with node.default, ref: docs.chef.io/deprecations_attributes.html, I used same solution except changing node.set as per deprecation warning.