Forge Home




9,911 latest version

2.6 quality score

We run a couple of automated
scans to help you access a
module's quality. Each module is
given a score based on how well
the author has formatted their
code and documentation and
modules are also checked for
malware using VirusTotal.

Please note, the information below
is for guidance only and neither of
these methods should be considered
an endorsement by Puppet.

Version information

  • 0.0.1 (latest)
released Apr 10th 2014
This version is compatible with:
  • Puppet 3.x
  • , , , , , , ,

Start using this module

  • r10k or Code Manager
  • Bolt
  • Manual installation
  • Direct download

Add this module to your Puppetfile:

mod 'treydock-dynamic_hosts', '0.0.1'
Learn more about managing modules with a Puppetfile

Add this module to your Bolt project:

bolt module add treydock-dynamic_hosts
Learn more about using this module with an existing project

Manually install this module globally with Puppet module tool:

puppet module install treydock-dynamic_hosts --version 0.0.1

Direct download is not typically how you would use a Puppet module to manage your infrastructure, but you may want to download the module in order to inspect the code.

Tags: hosts, dynamic


treydock/dynamic_hosts — version 0.0.1 Apr 10th 2014


Build Status

####Table of Contents

  1. Overview - What is the dynamic_hosts module?
  2. Usage - Configuration and customization options
  3. Use Cases - Real world usage scenerios?
  4. Development - Guide for contributing to the module


The dynamic_hosts module lets you create dynamic host entries based on a system's IP addresses.


Class: dynamic_hosts

By default the class performs no actions. The dynamic_hosts_entries top-scope variable can be used to define dynamic_hosts::entry resources.

class { 'dynamic_hosts': }

Example of a top-scope dynamic_hosts_entries variable

$dynamic_hosts_entries = {
  'example.local' => { 'ip_networks' => [
    {'ip' => '', 'network' => ''},
    {'ip' => '', 'network' => ''}],

The class can be defined with the entries parameter to also create dynamic_hosts::entry resources

class { 'dynamic_hosts':
  entries  => {
    'example.local'  => { ip_networks => [
       {'ip' => '', 'network' => ''},
       {'ip' => '', 'network' => ''}],

Parameters for dynamic_hosts class


The Hash that is passed to create_resources to define dynamic_hosts::entry resources (defaults to an empty Hash).

The dynamic_hosts_entries top-scope variable can be used in place of this parameter. The parameter's value takes presedence over the top-scope variable.

Defined type: dynamic_hosts::entry

Defines a /etc/hosts entry based on the ip_networks Hash. If the system has an IP within the value of the network Hash element, then the ip value is assigned to the host entry.

The following example creates a /etc/hosts entry for example.local with the ip value from the first hash with a matching network value.

dynamic_hosts::entry { 'example.local':
  ip_networks => [
    {'ip' => '', 'network' => ''},
    {'ip' => '', 'network' => ''}

Aliases can also be defined

dynamic_hosts::entry { 'example.local':
  ip_networks => [
    {'ip' => '', 'network' => ''},
    {'ip' => '', 'network' => ''}
  host_alises => ['example-foo.local', 'example-bar.local'],

Parameters for dynamic_hosts::entry defined type


The defined type's name that sets the hostname of the /etc/hosts entry.


Array of Hashes where each Hash has the ip of the host entry that's assigned if an interface matches the network value. A Hash with network set to can be used as the "catch all".


Sets the /etc/hosts entry's FQDN (defaults to namevar).


Array of aliases for the /etc/hosts entry (defaults to undef).


Controls the existance of the /etc/hosts entry (defaults to 'present').

Function: find_ip_by_network

Returns the IP of the first network match found from an Array of Hashes. Returns false if no match is found.

Each hash must have an ip key and a network key.

Examples for find_ip_by_network function

In these examples, the puppet client has eth0 with network and lo with

$ip = find_ip_by_network({'ip' => '', 'network' => ''})

Would return:

$ip = find_ip_by_network({'ip' => '', 'network' => ''}, {'ip' => '', 'network' => ''})

Would return:

$ip = find_ip_by_network({'ip' => '', 'network' => ''})

Would return: false

  • Type: rvalue

Use Cases

LDAP Automount


A single infrastructure shares LDAP and a NFS server for home directories. Multiple data centers share these resources in seperate buildings.

All user home directories are automounted based on a single LDAP auto.home entry. Some of the systems are on a high-speed private network local to the NFS server, while others must use a 'public' network to access the NFS server.

NFS Server IPs:

  • eth0: (public LAN) (
  • eth1: (
  • lo:

Client1 networks:

  • eth0: (public LAN) (
  • eth1: (
  • lo: (

Client2 networks:

  • eth0: (public LAN) (
  • eth1: (
  • lo:


In LDAP set the hostname for the NFS automount to something like automount-nfs-home.local then use this module to map that hostname to an IP based on the IPs available to each server.

The following would be defined in Puppet (this example is in YAML for readability).

      - ip: ''
        network: ''
      - ip: ''
        network: ''

In the example above Client1 would be assigned this host entry  automount-nfs-home.local

'Client2' would be assigned this host entry automount-nfs-home.local


This module has been built on and tested against Puppet 2.7 and higher.

This module has been tested on:

  • CentOS 5/6
  • Scientific Linux 5/6



Testing requires the following dependencies:

  • rake
  • bundler

Install gem dependencies

bundle install

Run unit tests

bundle exec rake test

If you have Vagrant >= 1.2.0 installed you can run acceptance tests

bundle exec rake acceptance