# What's the preferred way of geo-locating an agent node?

Suppose, for the sake of argument, I managed a globally distributed infrastructure making heavy use of Debian. I'd like for each of my machines to be able to use the local Debian mirror as its apt repository source.

What I would ideally like to be able to do is

• determine the IP address that my agent uses to hit my puppetmaster (possibly NATed IPv4, hence unrelated to the $ipaddress fact if I understand the implications of the latter correctly), • then use a custom function to wrap a GeoIP lookup library (possibly geoip) to determine the node's country, • stick ... edit retag close merge delete ## 2 Answers Sort by » oldest newest most voted Thanks Joe. Thanks in particular for pointing me to facter-dot-d, which in combination with a JSON capable geoip service actually makes this really easy. class location ($url = "http://freegeoip.net/json/", $filename = "location.json" ) { exec { "wget -q$url -O /etc/facter/facts.d/$filename": creates => "$/etc/facter/facts.d/\$filename",
path => ["/bin", "/sbin", "/usr/bin", "/usr/sbin"]
}
}


The use of creates ensures that we do this only once per node. You can obviously polish this to make sure that the directory exists, etc., but the snippet above illustrates the concept. And as long as the json gem (or ruby-json or ...

more

That's really slick. :)

( 2013-01-10 16:29:29 -0500 )edit

There's now a module for this on the Forge at http://forge.puppetlabs.com/hastexo/location.

( 2013-01-13 15:58:11 -0500 )edit

Hi Florian,

If the use-case is specific to just apt-mirrors, you could create a shell script that runs something like netselect-apt or apt-spy. Then execute that script through an exec resource and only have it run if a control file such as /var/lib/apt/dont-run-me doesn't exist. You could do this on each node or designate a single server at each location to be an apt-cacher server and have all nodes at each location connect to the designated server.

To expand the use-case to account for other location-based scenarios, one way to do this is to create a ...

more