16

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

SASI
  • 475
  • 2
  • 7
  • 16
  • 1
    please add some code so we can see the problem you're facing – dax Apr 18 '15 at 19:16
  • Seems like a question for https://serverfault.com/, yet still not clear what you want. – Fionn Apr 19 '15 at 04:09
  • 2
    The actual problem seems to be `command = 'cat #{fileName}'` where @SASI should have used double quotes to enable parsing (ie. `command = "cat #{fileName}"`) – scones Jun 27 '16 at 11:08

1 Answers1

24

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.

Community
  • 1
  • 1
StephenKing
  • 36,187
  • 11
  • 83
  • 112
  • This IMHO at least answers your question. If you describe your problem, we might find a way more elegant solution. – StephenKing Apr 18 '15 at 19:44
  • 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. – SASI Apr 19 '15 at 10:07
  • 1
    @user3886612 Edit your question to describe the use case, so we can cast a re-open vote – Tensibai Apr 20 '15 at 09:31
  • 1
    @SASI repeating yourself in comments won't help. there's an edit button under your question to update it. – Tensibai Apr 21 '15 at 08:05
  • 1
    It would be also interesting to see, in what way you have to use that information later. – StephenKing Apr 21 '15 at 11:46
  • What does Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut) do in this? – Elan Hasson Mar 16 '17 at 16:39
  • This makes the functions of `Chef::Mixin::ShellOut` available within the `ruby_block`. – StephenKing Mar 18 '17 at 10:31
  • There is deprecation warning for using `node.set`, that is being replaced with `node.default`, ref: https://docs.chef.io/deprecations_attributes.html, I used same solution except changing `node.set` as per deprecation warning. – Deepak Deore May 21 '18 at 20:16