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

How to provide attribute value in the template .erb file

如何在模板 .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 文件中访问属性文件中定义的属性?

谢谢

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

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

然后你需要使用 Erb 格式,你有点做了但不是真的:

<%= @hostname %>

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