how to set node attribute value from execute resouce command output

放肆的年华 提交于 2020-01-16 19:19:08

问题


execute 'my_commad' do
  command 'myvar=`curl http://example.com/blah/blah` && echo -n $myvar > /etc/value'
end

node.default[:attribute1] = ::File.read('/etc/value').chomp

This will fail, because at the time of convergence the node attributes are checked first and hence it will throw the following error:

ERROR: No such file or directory @ rb_sysopen - /etc/value


回答1:


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.



来源:https://stackoverflow.com/questions/57146885/how-to-set-node-attribute-value-from-execute-resouce-command-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!