Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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:

this=that
location=$LOCATION_NUMBER
bananas=tasty

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?

Thanks.

share|improve this question

2 Answers 2

up vote 11 down vote accepted

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:

this=that
location=<%= LOCATION_NUMBER %>
bananas=tasty

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 %>
custom_config=true
<% else %>
custom_config=false
<% 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.

share|improve this answer
    
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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