2017-10-07 41 views
0

我得到的模板错误是由于backend.key=<%= node['key']%>源key.properties.erb>在运行shellout时没有值。无法在运行时设置节点属性,并将其引用到厨师模板中

Error : Chef::Mixin::Template::TemplateError - undefined method `[]' for nil:NilClass 

我有一个红宝石块获取文件cat /tmp/key.txt和节点值分配的输出。

红宝石块:

ruby_block "Get_key" do 
    block do 
     #tricky way to load this Chef::Mixin::ShellOut utilities 
     Chef::Resource::RubyBlock.send(:include, Chef::Mixin::ShellOut)  
     command = 'cat /tmp/key.txt' 
     command_out = shell_out(command) 
     node.set['key'] = command_out.stdout 
    end 
    action :create 
end 

厄尔布:

backend.key=<%= node['key']%> 

回答

0

没有必要shell_out读取文件的内容。试试这个:

ruby_block "Get_key" do 
    only_if { node['key'] == "" } 
    block do 
    node.set['key'] = File.read('/tmp/key.txt') 
    end 
end 

但我觉得你的实际的问题是其他地方。错误消息表明您的模板中有nodenil,这很不寻常。

因此,无论我是否盲目,并且您在发布的模板行中确实存在一些错字,或者以简化代码示例的方式隐藏了错误。我假设你的真实模板看起来更像

backend.key=<%= node['foo']['key']%> 

foo不是数组。检查一下。

+0

我做的一个非常类似的事情[这里](https://github.com/TYPO3-cookbooks/gerrit/blob/fc82b9d82663f040116aed62bdcc3991e858ab98/recipes/peer_keys.rb) – StephenKing

0

请不要使用这种模式。它很慢,并将额外的数据放在节点对象中,占用空间和内存,并使搜索索引变得很难受。你想要的是:

template "whatever" do 
    # Other stuff ... 
    variables my_file: lazy { IO.read('/tmp/key.txt') } 
end 

这会延迟读取直到收敛时间。

+0

你好,我想你的方法,得到了下面的值在模板中: backend.key =# Aby

+0

我改变了如下所示的模板erb: 后端。 key = <%= @key%> – Aby

+0

你在使用厨师13吗?如果不是,那么'变量懒惰{{key:IO.read(...)}}'。 – coderanger