I have several machines I am managing via puppet. The machines are at different physical locations. Each location has a specific location number.

I have a config file that needs to have the correct location number in it.

So the contents of /etc/abc.conf are like this:


When the file gets pushed down to the machine, I need $LOCATION_NUMBER to actually be the location number.

Can I manage this file with puppet and how?

If I export a facter for LOCATION_NUMBER, can puppet write the correct location number when it pushes the file down to the machine?



What you're looking to do is called a template. It's a core feature of Puppet, and very useful. You write a file in eRB format that can use Ruby code and Facter variables to build a file. Here's an example:

file { '/etc/my_config_file':
  ensure => present,
  owner => 'root',
  group => 'root',
  mode => 0700,
  content => template('module/my_config_file.erb'),

Now, in your module hierarchy under templates/ (not files/), place a file my_config_file.erb:

location=<%= LOCATION_NUMBER %>

The <%= %> format says to execute some Ruby code and return the result. In this case, all facts are available as local variables, like ipaddress, lsbmajdistrelease or LOCATION_NUMBER(your custom fact). You can also use any other Ruby code that doesn't directly return a result inside <% %>, such as if statements:

<% if LOCATION_NUMBER == 7 %>
<% else %>
<% end %>

Edit: As I re-read this answer, I'd like to suggest that you don't use capital letters for your Fact name. In Ruby, a variable that starts with a capital letter is immutable. While I'm not sure this is really an issue, it goes against convention.

  • I'll have to do some research into templates, but this is just what I was looking for. Thanks! – Ed Manet Mar 23 '12 at 14:58

Additionally, you can store the $LOCATION_NUMBER variable as a parameter to be returned by a ENC . You use the ENC so that hosts in a particular location( or domain), or group or hostname, get a particular variable. There are several good, free ENCs pre-built and available, including Dashboard and The Foreman.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.