Puppet ERB模板:如何将变量注入语句

时间:2021-11-05 22:30:47

I'd like to make some modifications on variable in erb templates: e.g. decode base64 and split string

我想对erb模板中的变量进行一些修改:例如解码base64和拆分字符串

It tried

<%= Base64.decode64(@my_variable).rpartition(':').last %>

and

<%= Base64.decode64(<%= @my_variable %>).rpartition(':').last %>

and via scope

并通过范围

<%= Base64.decode64(scope['my_class::my_variable']).rpartition(':').last %>

but it is nil.

但它没有。

 Filepath: /usr/lib/ruby/1.9.1/base64.rb
  Line: 58
  Detail: undefined method `unpack' for nil:NilClass

puppet version 3.8.2

木偶版3.8.2

1 个解决方案

#1


2  

The first example that you tried is the correct ERB notation.

您尝试的第一个示例是正确的ERB表示法。

That error would be occurring because you passed the nil object to Base64.decode64():

由于您将nil对象传递给Base64.decode64(),因此会发生该错误:

[1] pry(main)> require 'base64'
=> true
[2] pry(main)> Base64.decode64(nil).rpartition(':').last
NoMethodError: undefined method `unpack' for nil:NilClass
from /Users/alexharvey/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/base64.rb:58:in `decode64'

(Admittedly, I don't have your Ruby 1.9.1 and I wasn't able to easily install it, but it's unlikely this changed.)

(不可否认,我没有你的Ruby 1.9.1而且我无法轻松安装它,但这不太可能改变。)

That means that @my_variable is set to nil, and that would happen if you failed to actually set the Puppet variable $my_variable in your manifest.

这意味着@my_variable设置为nil,如果您未能在清单中实际设置Puppet变量$ my_variable,则会发生这种情况。

So you would need to call the template with a block something like this:

所以你需要用这样的块来调用模板:

  $my_variable = 'foo'
  file { '/tmp/foo':
    ensure  => file,
    content => template('test/mytemplate.erb')
  }

#1


2  

The first example that you tried is the correct ERB notation.

您尝试的第一个示例是正确的ERB表示法。

That error would be occurring because you passed the nil object to Base64.decode64():

由于您将nil对象传递给Base64.decode64(),因此会发生该错误:

[1] pry(main)> require 'base64'
=> true
[2] pry(main)> Base64.decode64(nil).rpartition(':').last
NoMethodError: undefined method `unpack' for nil:NilClass
from /Users/alexharvey/.rvm/rubies/ruby-2.1.4/lib/ruby/2.1.0/base64.rb:58:in `decode64'

(Admittedly, I don't have your Ruby 1.9.1 and I wasn't able to easily install it, but it's unlikely this changed.)

(不可否认,我没有你的Ruby 1.9.1而且我无法轻松安装它,但这不太可能改变。)

That means that @my_variable is set to nil, and that would happen if you failed to actually set the Puppet variable $my_variable in your manifest.

这意味着@my_variable设置为nil,如果您未能在清单中实际设置Puppet变量$ my_variable,则会发生这种情况。

So you would need to call the template with a block something like this:

所以你需要用这样的块来调用模板:

  $my_variable = 'foo'
  file { '/tmp/foo':
    ensure  => file,
    content => template('test/mytemplate.erb')
  }