如何在模板.erb文件中提供属性值

时间:2018-08-15 03:26:50

标签: ruby chef cookbook

如何在模板.erb文件中提供属性值

qradar_logs/
├── Berksfile
├── LICENSE
├── README.md
├── attributes
│   └── default.rb
├── chefignore
├── files
│   └── default
│       └── default
├── metadata.rb
├── recipes
│   └── default.rb
├── spec
│   ├── spec_helper.rb
│   └── unit
│       └── recipes
│           └── default_spec.rb
├── templates
│   └── rsyslog.conf.erb
└── test
    └── integration
        └── default
           └── default_test.rb

11 directories, 12 files

attribute.rb文件中,我具有以下内容:

default['logs']['hostname'] = "169.67.89.72:514"

在我的食谱中,我提供了以下内容:

hostname = node['logs']['hostname']

在我的模板rsyslog.conf文件中,我想根据attributes文件中更改的值使用该值。

我尝试给:

<%= "#{hostname}" %>

错误提示为:

FATAL: Chef::Mixin::Template::TemplateError: undefined local variable or method `hostname'

如何访问template.erb文件中的属性文件中定义的属性?

谢谢

1 个答案:

答案 0 :(得分:0)

几件事。首先,您应该在variables资源中使用template属性,以将数据公开给模板:

template '/whatever' do
  # ...
  variables hostname: node['logs']['hostname']
end

然后,您需要使用Erb格式,但您确实没有这样做:

<%= @hostname %>

变量名与您在variables中使用的键匹配。