Module for managing /etc/hosts
Justin Lambe

Justin Lambe



8,304 latest version

4.8 quality score

Version information

  • 1.1.1 (latest)
  • 1.1.0
  • 1.0.0
released Jul 3rd 2019
This version is compatible with:
  • CentOS

Start using this module


justinjl/hosts — version 1.1.1 Jul 3rd 2019



A puppet module to manage the contents of hosts files.

Module Description

This module can be used to manage the contents of the hosts file. In addition to user supplies entries, it can also automatically create entries for the loopback interfaces (ipv4 and ipv6) as well.


What hosts affects

  • This module will make changes to your hosts file - /etc/hosts for most operating systems.

Beginning with hosts

Declare the class and add entries as follows:

class { '::hosts' : }

::hosts::add { '' :
    fqdn    => '',
    aliases => [ 'router' ]



Declare the class. There are a number of optional parameters whose defaults are listed below:

class { '::hosts' :
    file        => '/etc/hosts',
    owner       => 'root',
    group       => 'root',
    mode        => '0644',
    localhost   => true,
    primary     => true,
    purge       => false,

Parameters within hosts:

  • file: Optional. Path to hosts file. Default value varies depedning on Operating System.
  • owner: Optional. User that has ownership of the hosts file. Defaults to root.
  • group: Optional. Group that has group ownership of the hosts file. Defaults to root.
  • mode: Optional. File permissions mode for the hosts file. Defaults to 0644.
  • localhost: Optional. Add hosts file entries for localhost/loopback interfaces. Defaults to true.
  • primary: Optional. Add host file entries for primary fqdn & hostname to primary IP Address. Defaults to true.
  • purge: Optional. Remove unmanaged entries from the hosts file. Defaults to false.


Create hosts file entries as follows:

::hosts::add { '' :
    fqdn    => '',
    aliases => [ 'router' ],

Parameters with hosts::add:

  • fqdn: Required. Fully qualified domain for the hosts file entry. You could use os plain hostname if need be.
  • aliases: Optional. String or array containing aliases for the hosts file entry.


Public classes

  • hosts: The main class used to interact with this module.

Private classes

  • hosts::file: Class to handle declare the file resource and set the ownership and permissions.
  • hosts::localhost: Class to add localhost entries to the hosts file.
  • hosts::params: Class to store default parameter values and determine OS specific values.
  • hosts::primary: Class to add hosts file entries for the primary interface (::ipaddress -> ::fqdn/::hostname).


  • hosts::add: Type to create hosts file entries.


This module has been made for (osfamily) Debian and RedHat (and their derivatives), however it should work fine on any 'nix. You may need to manually specify the path to the hosts file, and you will need sed.


Appreciate any suggestions on feature or code changes. Let me know if you want to contribute or collaborate.