Something like this in $template.erb:
<VirtualHost <%= ip %>:443>
Document Root <%= docroot %>
SSLCertificateFile <%= cert %>
# ...
</VirtualHost>
That is, any variable in puppet is available as a local variable in the ruby bits hiding in the .erb file.
Not sure why you have "$template" instead of a named .erb file (not seeing $template set anywhere, but willing to assume it's hiding somewhere)
And this seems wrong: cert => Openssl::Cert["rri"]
. I'd expect something more like:
openssl::cert { "rri": }
And then the .erb would be more like:
<VirtualHost <%= ip %>:443>
Document Root <%= docroot %>
SSLCertificateFile <%= certDirectory %>/<%= name %>
# ...
</VirtualHost>
So, to answer your question directly, I don't believe getting a list of exported resources directly from erb is possible. This is due to the nature of exported resources. To Puppet, they're just more resources that need to be created on the host.
But, there is a way to accomplish what you're looking to do. I do it in a few places in my environment.
Here we create a directory of files, one for each host we want to flag as a "bacula_client". We use the purge
, force
, and recurse
options to remove files that are not managed by Puppet (i.e. if you want to remove a system from this "list").
class bacula::client {
@@file { "/etc/bacula_clients/$fqdn":
ensure => present,
content => "",
require => File['/etc/bacula_clients'],
tag => "bacula_client",
}
}
class bacula::server {
#
# .. include whatever else the server requires, like package {} file {} service {}
#
file { "/etc/bacula_clients":
ensure => directory,
purge => true,
recurse => true,
force => true,
}
# Populate directory of client files.
File <<| tag == "bacula_client" |>>
}
Next, we use some Ruby code in the .erb to scan this directory for files, and act on them:
<%
bacula_clients_dir = '/etc/bacula_clients'
d = Dir.open(bacula_clients_dir)
# Remove directories from the list of entries in the directory (specifically '.' and '..'):
backupclients = d.entries.delete_if { |e| File.directory? "#{bacula_clients_dir}/#{e}" }
backupclients.each do |backup_files|
-%>
Job {
Name = "Server"
JobDefs = "DefaultJob"
Client = <%= backup_files %>-fd
Pool = TeraMonth
Write Bootstrap = "/var/lib/bacula/<%= backup_files %>.bsr"
}
<% end -%>
Best Answer
I was in the exact same situation some time back. Basically, for erb templates to be used by puppet, you do not require much knowledge of Ruby. But, that also depends on the complexity and purpose of your puppet modules.
If for example, you are mainly going to use them for configuration files for server management, then mostly you'd be needing to manipulate things as ip addresses and hostnames. And the most commonly used Ruby functions you'd be encountering are
split
andjoin
.The easiest way to mess around with these and other functions would be to install ruby and its interactive shell, irb and test your code. So...a cheat sheet, I don't know. I bugged folks over at stack overflow and #ruby on irc ! I have a bunch of templates here https://github.com/alcy/pupmods/tree/master/puppet/templates/ ( badly organized ! ), that might help you.
Regarding the case statement query, you can have something like this ( one of the possible approaches ) :
The template part of it is explained as: split the ipaddress of the client machine obtained from facter at each occurrence of a DOT ".", collect the first three numbers, and join them with a DOT in between each element. Store this as variable
ip1
. According to this value, serve the necessary resolv.conf file from puppet's file server.