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
Http Library for Ruby with Https, Ssl Client Certificate and Keep-Alive Support
Testing Routes with Subdomain Constraints Using Rspec
How to Get a Selenium/Ruby Bot to Wait Before Performing an Action
What Is the Purpose of 'Kernel'
Changing Songs on Jplayer by Clicking a Link, Hosted on Amazon S3
Storing Passwords for External APIs - Best Practice
Problem Using Openstruct with Erb
How to Specify the Location of the Chromedriver Binary
Rails: Methods Shared by Multiple Controllers
If(!X) VS If(X==False) in Ruby
Mailchimp API Not Replacing Mc:Edit Content Sections (Using Ruby Library)
Rails ( Set_No_Cache Method) Cannot Disable Browser Caching in Safari and Opera
Ruby Variable Name with Double Underscores
Activerecord Connection Warning. (Database Connections Will Not Be Closed Automatically)
How to Read Only X Number of Bytes of the Body Using Net::Http