Ask Your Question

creating 'location' fact

asked 2016-10-21 11:07:59 -0600

gene gravatar image

I'm relatively new to puppet and have a puppet server up and some agents deployed on some test nodes. We have servers in two different locations (at a colo facility and on campus). I'd like to be able to take actions in some of my classes based off of the location (e.g. either 'colo' or 'campus'). I can determine the location based upon the server IP address but i'm not sure how to actually put that into practice.

I'm reading up on hiera but definitely don't have my head wrapped around that yet. I'm thinking that I should somehow define in hiera the list of subnets in use in each of the two locations and associate those with the correct location.

For example, if my subnet to location mapping is this:, => location = colo, => location = campus

I'm not sure how to translate that into something useable inside my common.yaml. My common.yaml (/etc/puppetlabs/code/hieradata/common.yaml) currently looks like this:

ntp::autoupdate: true
ntp::enable: true

profile::puppet::runinterval: 600

How would I code that in common.yaml? Is this the best way to determine 'location' of a node? Any help is most appreciated.


edit retag flag offensive close merge delete


Location mapping should be made via your network switches, not via IP-addresses. Where your computers are wired determine their location, not their addresses.

Kai Burghardt gravatar imageKai Burghardt ( 2016-10-21 18:15:06 -0600 )edit

Thanks for the comment Kai. I was looking to determine the location of my nodes and the easiest way I could think of to do that was based upon the node's IP address. Based upon the subnet the node resides in, I can then set the appropriate root password. That was my goal.

gene gravatar imagegene ( 2016-10-24 09:50:10 -0600 )edit

Ah, OK, in that case, the approach lavaman suggested is sufficient.

Kai Burghardt gravatar imageKai Burghardt ( 2016-10-24 16:25:15 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2016-10-21 13:37:19 -0600

lavaman gravatar image

updated 2016-10-21 13:37:46 -0600

You wouldn't put that in your common.yaml. You would create new hiera files for each location with the appropriate values and put that into your hiera config.

As far as determining the location from IP, you can either write a custom fact or you can just put some logic in your site.pp that sets a global variable (which hiera can also use like fact values).

Site.pp is the easier way (doesn't require ruby code):

    if $::ipaddress =~ /^192\.168\.[1,2]/ {
      $location = colo
    } else {
      $location = campus

If you have or add more subnets, you can also do an elsif with another regex.

edit flag offensive delete link more


Thanks lavaman! I did as you suggested in site.pp to set the location. I then used that in a profile::common::root class to set the root password based on the location. We use a different root password for our colo servers.

gene gravatar imagegene ( 2016-10-24 09:45:46 -0600 )edit

With a custom fact to output your location, you would be able to add a 'location' tier in your hiera config, and put the specific configuration in a hieradata/location/colo.eyaml (for passwords) and a campus.eyaml for your other locations, and there you store your 'location' specific information

DarylW gravatar imageDarylW ( 2016-10-24 10:54:50 -0600 )edit

That works whether it's a global var from site.pp or a custom fact.

lavaman gravatar imagelavaman ( 2016-10-24 12:39:08 -0600 )edit

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: 2016-10-21 11:07:59 -0600

Seen: 114 times

Last updated: Oct 21 '16