How to Get a Linux Command Output to Chef Attribute

How to get a linux command output to chef attribute

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.

how to set node attribute value from execute resouce command output

if you are setting the node attribute at the recipe itself and not in an attribute file, this might work for you. though, in my opinion, there is a better way to do so...

you can use ruby_block and use ruby code to fetch the value that you need from the url and then assign it to a node attribute. it will be something like:

ruby_block 'fetch value' do
block do
require 'net/http'
require 'uri'

url = 'http://example.com/blah/blah'
val = Net::HTTP.get(URI.parse(url))
node.default[:attribute1] = val
end

just make sure that every read for the attribute1 node attribute, is taking place after it was assigned. you might need to rearrange the node run-list to make sure that all recipes that depend on this recipe, will be appear in the node run-list after the recipe above was executed.

Chef - How to get the output of a command to a Ruby variable

Chef is Ruby so you can use backticks output = `find /filder1 | grep .txt`

Chef - Get output of remote_execute resource

You can't, Chef resources don't generally have outputs. In some cases the support an output API (like the AWS provisioning driver's aws_object helpers) but for something like this you would need to get the low-level Machine object and call its execute method. Take a look at how the resource is implemented for an example. You might also want to skip Provisioning's transport layer and use Train as we are probably going to try and centralize on that library.



Related Topics



Leave a reply



Submit