Ask Your Question

Using Puppet Templates

asked 2015-03-10 16:21:26 -0500

Cesar Sturion gravatar image


I'm setting up an environment with puppet and I'm installing it some applications which serves as a support for some applications.

They are tools created for us for example file import for a database.

In this case this tool have a file called connectionbroker.ini, with the following structure:

password_automatic = N
password_encrypted = S
password_manager = com.softdes.sql.pwd.CipheredPwdMgr
user = Owner01
url = jdbc:oracle:thin:@_ip_server:_port:_database

password_automatic = N
password_encrypted = S
password_manager = com.softdes.sql.pwd.CipheredPwdMgr
user = Owner02
url = jdbc:oracle:thin:@_ip_server:_port:_database

factories = pool

class = pooled
contexts = Owner03 Owner02 Owner01

password_automatic = N
password_encrypted = S
password_manager = com.softdes.sql.pwd.CipheredPwdMgr
user = Owner03
url = jdbc:oracle:thin:@_ip_server:_port:_database

I thinking to create a template to solve this, but how to do this ? Someone has a example ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-03-11 04:34:58 -0500

Jeff Johnson gravatar image

updated 2015-03-11 12:37:35 -0500

I had to create some templates for config files awhile back. The details are a little fuzzy, but at a high level:

  • Create a .erb template that describes your ini file.
  • For the values you want to replace, use the ERB syntax to describe the placeholders you need. This stackoverflow answer has a good, brief description of the basics of ERB syntax.
  • Populate the placeholders with values set elsewhere in your puppet manifest.


I had a similar situation where I had to configure a rails application. Here's how I approached it:

  • I created a .erb file for my database.yml file (which sets all the connection information for the program's database). It looks like this:


adapter: mysql2
  database: <%= scope['::railsapp::mysql_db_name'] %>_development
  username: <%= scope['::railsapp::mysql_db_user'] %>
  password: <%= scope['::railsapp::mysql_db_user_password'] %>
  host: <%= scope['::railsapp::mysql_host_name'] %>
  port: <%= scope['::railsapp::params::mysql_port'] %>
  # repeat for test and production environments
  • Notice the scope keyword in the erb? That allows us to look up the value of variables in our puppet manifest. Puppet's variable lookup rules can be found here.

How did those variables I'm referencing in the database.yml.erb get there? I set them in my manifest's init.pp, which looks a bit like this:


class railsapp (
    $rvm_version                = $railsapp::params::rvm_version,
    $rvm_ruby_version           = $railsapp::params::rvm_ruby_version,

    $mysql_host_name            = $railsapp::params::mysql_host_name,
    $mysql_db_name              = $railsapp::params::mysql_db_name,
    $mysql_db_user              = $railsapp::params::mysql_db_user,
    $mysql_db_user_password     = $railsapp::params::mysql_db_user_password,
    $mysql_db_user_grants       = $railsapp::params::mysql_db_user_grants,
    $mysql_backup_dir           = $railsapp::params::mysql_backup_dir,
    $mysql_backup_user          = $railsapp::params::mysql_backup_user,
    $mysql_backup_user_password = $railsapp::params::mysql_backup_user_password,
    $mysql_backup_compress      = $railsapp::params::mysql_backup_compress,
    $mysql_port                 = $railsapp::params::mysql_port,

    # other variables/parameters that need to be initialized are omitted for clarity...
) inherits railsapp::params {
    # Do puppet stuff here.
  • The init.pp has all of our parameters initialized to some value (i.e. whatever value is stored in our params.pp class).
  • The ::railsapp::params is another class (in a params.pp file) that just holds all the default values for my railsapp class. I've omitted it for [hopefully] clarity.
  • The scope in our database.yml.erb file can look at ::railsapp::[whatever parameter we need]. Replace the [whatever parameter we need] with one of the parameters that was set in our init.pp.

A Second (and Simpler) Template Example

When initially testing puppet, I set up a bunch of VMs using Vagrant. I used a template to set up the /etc/hosts file for each VM so they could all talk to each other. The template looks like this:

host_file.erb:   <%= scope['::fqdn'] %> <%= scope['::hostname'] %> localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

<%= "" unless scope['::ipaddress_eth1'] == "" %>
<%= "" unless scope['::ipaddress_eth1'] == "" %>
<%= "" unless scope['::ipaddress_eth1'] == "" %>
<%= "" unless scope['::ipaddress_eth1'] == "" %>
<%= " ...
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-03-10 16:21:26 -0500

Seen: 325 times

Last updated: Mar 11 '15