6

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.

Ed Manet
  • 532
  • 1
  • 5
  • 17

2 Answers2

11

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.

Kyle Smith
  • 9,683
  • 1
  • 31
  • 32
0

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.

Not Now
  • 3,552
  • 18
  • 19