Venkat Sudharsanam
Venkat Sudharsanam

Reputation: 5

Linux Command Output to Chef Attribute

The issue is I need to assign the value of Linux command to CHef Attribute.But unable to do it.

Im using the below code and not finding the result. Kindly Help what im 
missing

ruby_block "something" do
  block do
    Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)
    node.default['foo'] = shell_out("echo Hello world").stdout
  end
  action :create
end



log "demo" do
  message lazy { node['foo'] }
end

Below is the Run logs:

Starting Chef Client, version 13.9.1
resolving cookbooks for run list: ["sample_repo"]
Synchronizing Cookbooks:
  - sample_repo (0.1.4)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 2 resources
Recipe: sample_repo::default
  * ruby_block[something] action create
    - execute the ruby block something
  * log[demo] action write


Running handlers:
Running handlers complete
Chef Client finished, 2/2 resources updated in 02 seconds

Thanks in advance

Upvotes: 1

Views: 1145

Answers (1)

coderanger
coderanger

Reputation: 54249

Your code is fine, the log message is not showing because the default level on the log resource is :info and by default chef-client doesn't show info-level log messages when run interactively. That said, this kind of code where you store stuff in node attributes is very brittle and probably shouldn't be used unless specifically needed. Better is to do this:

message lazy { shell_out("echo Hello world").stdout }

Also you don't need any funky mutating include stuff like you there AFAIK, the shell_out helpers are available in most contexts by default. Also you should usually use shell_out!() rather than shell_out(), the ! version automatically raises an exception if the command fails. Unless you specifically want to allow a failed command, use the ! version.

Upvotes: 1

Related Questions