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.
Related videos on Youtube
Author by
SASI
Updated on July 09, 2022Comments
-
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 about 9 yearsplease add some code so we can see the problem you're facing
-
akrishnamo about 9 yearsSeems like a question for serverfault.com, yet still not clear what you want.
-
scones almost 8 yearsThe actual problem seems to be
command = 'cat #{fileName}'
where @SASI should have used double quotes to enable parsing (ie.command = "cat #{fileName}"
)
-
-
StephenKing about 9 yearsThis IMHO at least answers your question. If you describe your problem, we might find a way more elegant solution.
-
SASI about 9 yearsThanks 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 about 9 years@user3886612 Edit your question to describe the use case, so we can cast a re-open vote
-
Tensibai about 9 years@SASI repeating yourself in comments won't help. there's an edit button under your question to update it.
-
StephenKing about 9 yearsIt would be also interesting to see, in what way you have to use that information later.
-
Elan Hasson about 7 yearsWhat does Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut) do in this?
-
StephenKing about 7 yearsThis makes the functions of
Chef::Mixin::ShellOut
available within theruby_block
. -
Deepak Deore almost 6 yearsThere is deprecation warning for using
node.set
, that is being replaced withnode.default
, ref: docs.chef.io/deprecations_attributes.html, I used same solution except changingnode.set
as per deprecation warning.