0

erb

$names = ['foo','boo','wee','haa']
<% @names.each do |name| %>
Hello <%= name %>
he is here
<% end %>

config.pp

file {'/tmp/tester':
    ensure=>'present',
    content=>template("test/hello.erb"),
}

Error when running puppet agent -t

 Filepath:  /etc/puppetlabs/code/environments/development/modules/test/tem plates/hello.erb
 Line: 2
 Detail: undefined method `each' for nil:NilClass

In google, some suggest I do [@name].each instead. puppet agent -t will run, but the /tmp/tester will look like the following:


cat /tmp/tester

$names = ['foo','boo','wee','haa']


Hello 
he is here

Question 2) So lets say we have a .conf file with a lot of content on it. I want to do.

If matches 'AllowUser*' #matching AllowUser and everything that comes after it. set 'AllowUser no' else add_line 'ALlowUser no'

1 Answers1

0

hello.erb is a template file.
That means that only code inside the tag markers is interpreted as ruby code.
Your variable assignment is not inside such tags and as a result of that, there is no $names variable declared.
There is no variable declaration in that file, it is simply a string on top that only gets written out.

As to your second question, there is probably some ways to do this but I simply wouldn't.
Puppet is meant as a way to describe the target system ("This is a webserver, it gets this configuration file with content X").
You generally cannot make decisions based on the clients files like this, as the server has no knowledge about them (remember: The catalog is getting compiled on the server!).
I'd rather do it right and template the file completely (or use a existing module that already does that).

faker
  • 17,496
  • 2
  • 60
  • 70