如何从执行资源命令输出中设置节点属性值 [英] how to set node attribute value from execute resouce command output

查看:71
本文介绍了如何从执行资源命令输出中设置节点属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:


错误:没有这样的文件或目录@ rb_sysopen-/ etc / value

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


推荐答案

如果要在配方本身而不是在属性文件中设置节点属性,则这可能对您有用。不过,在我看来,还有更好的方法...

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...

您可以使用 ruby​​_block 并使用ruby代码从url中获取所需的值,然后将其分配给节点属性。会是这样的:

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

只需确保对 attribute1 节点属性的每次读取都在分配之后进行。您可能需要重新排列节点运行列表,以确保所有依赖于此配方的配方都将在执行上面的配方后出现在节点运行列表中。

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.

这篇关于如何从执行资源命令输出中设置节点属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆