【问题标题】:How to use node attributes in a shell script in CHEF recipe如何在 CHEF 配方的 shell 脚本中使用节点属性
【发布时间】:2018-04-04 00:58:04
【问题描述】:

我创建了一个新的 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>

虽然配方成功,但我没有看到带有“hello world”的文件

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 节点[主][a2]

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

谢谢

【问题讨论】:

    标签: chef-infra chef-recipe cookbook


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      相关资源
      最近更新 更多