如何在CHEF配方中的shell脚本中使用节点属性

时间:2018-04-03 16:10:20

标签: chef chef-recipe cookbook

我创建了一个新的CHEF Coo​​kbook,在属性文件夹中,我将以下值添加到默认的attribute.rb文件中 ******** node.default['main']['a2'] = "hello world" ******** 在厨师食谱中,我想回应或创建一个名为" hello world"

的新文件

该食谱有以下几行:

食谱名称:: a2

# Recipe:: default<br>
#<br>
# Copyright (c) 2018 The Authors, All Rights Reserved.<br>
execute 'just_test' do <br>
command 'touch /tmp/a2345' <br>
end <br>
execute 'just_test1' do <br>
command "touch /tmp/node['main']['a2']" <br>
end <br>
execute 'just_test2' do <br>
command "echo node['main']['a2']" <br>
end <br>

虽然配方成功,但我没有看到带有&#34; hello world&#34;

的文件

recipe: a2::default
* execute[just_test] action run
- execute touch /tmp/a2345
* execute[just_test1] action run
- execute touch /tmp/node['main']['a2']
* execute[just_test2] action run
- execute echo node['main']['a2']
[2018-04-03T15:52:18+00:00] WARN: Skipping final node save because override_runlist was given

在tmp目录中创建以下文件

a2345节点[main] [a2]

我们如何在CHEF配方中进行属性替换? 是否有替代方案来实现此功能

谢谢

1 个答案:

答案 0 :(得分:2)

  1. 您不应使用execute来创建空文件,请使用file resource
  2. Chef是用Ruby和follows Ruby string interpolation编写的。因此,在您的情况下,它应该是"touch /tmp/#{node['main']['a2']}" - 外部双引号很重要。
  3. 您是否尝试过我们的official documentation?。你可以在那里了解这个以及其他有用的“案例”。