change host file depending on location

143 views
Skip to first unread message

Jim Dehune

unread,
Apr 3, 2012, 3:01:04 PM4/3/12
to Puppet Users
Hi all,

New to puppet and am hoping I can be pointed in the right direction.

I have several Amazon EC2 instance in us-east, us-west, eu, and apac.
I need different /etc/hosts file for each region. I'm not sure how to
implement this in each servers node definition.

I'd like to have something like this:

node server101 {
$location = us-west-1c
...
..
}

and have it setup the hosts file with the appropriate entries needed
from a template. facter has 'ec2_placement_availability_zone' which
has the location info I need.

What do I need to read more about to accomplish this in my classes.

Len Rugen

unread,
Apr 3, 2012, 3:20:42 PM4/3/12
to puppet...@googlegroups.com
This sounds like standard file template should work, for that file, use
   content => template("your-class/your-template.erb");

Then in that file (your-template.erb) you can have:

static stuff....
<% if location == "us-west-1c -%>
something
<% else %>
default stuff
<% end %>
more static stuff

The else is optional.  Syntax not guaranteed :-)



--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.


myeazel

unread,
Apr 4, 2012, 9:35:37 AM4/4/12
to puppet...@googlegroups.com
Depending on how complex the hosts file needs to be, what LenR may be best or I have sometimes also approached this kind of issue by using parameterized classes can work well too. You can use a define classname ($variables_to_pass_to_template) { } and then you don't have to work with ERB-type conditionals. Its a matter of preference in my opinion. So it would do something like this:

define location-based-hosts ($my_specific_hosts) {

file { "etc/hosts":
    ensure => present,
    content => template("puppet:///hosts/hosts_template.erb"),
    }
}

Then call it with:

hosts::location-based-hosts { "us-west":
    my_specific_hosts => "list of the important host entries",
}

And finally, in the hosts_template.erb:

127.0.0.1 localhost
<%= my_specific_hosts %>

The syntax should be good but I may have missed something. Again its all matter of preference but I have all my templates set up this way, that way my templates look as close to the end file as possible and all the "magic" happens in my manifests. 



Len Rugen

unread,
Apr 4, 2012, 9:30:43 PM4/4/12
to puppet...@googlegroups.com
We run Foreman, so I never think of parameterized classes :-) 
 
Another thing we do in some places is concatenated templates, there is a common section and different appendages. 

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/-ttLBhVQCiUJ.

jcbollinger

unread,
Apr 5, 2012, 9:19:02 AM4/5/12
to Puppet Users


On Apr 4, 8:30 pm, Len Rugen <lenru...@gmail.com> wrote:
> We run Foreman, so I never think of parameterized classes :-)


My esteem for Foreman has just gone up a notch. :)


John
Reply all
Reply to author
Forward
0 new messages