A collection of task oriented solutions in Puppet

 

Add a host entry

Challenge

You want to add an entry to your local systems equivalent to /etc/hosts

Solution

class host_entries {

  # create a simple hostname and ip host entry
  host { 'syslog':
    ip => '10.10.10.10',
  }

  # create a fully qualified full host entry with an alias
  host { 'ntpserver.example.com':
    ip           => '10.100.10.50',
    host_aliases => 'timeserver',
  }

  # host entry with multiple aliases
  host { 'dashboard':
    ip           => '10.120.100.111',
    host_aliases => [ 'nagios', 'munin' ],
  }

}

Explanation

The simplest use of the host type requires just a name (fully qualified or relative as needed) and the IP address it should resolve to. On the next puppet run in addition to your new host entry you'll also see the following Puppet warning banner, indicating that parts of this file are now under puppet management.

# HEADER: This file was autogenerated at 2017-08-12 11:12:09 +0100
# HEADER: by puppet.  While it can still be managed manually, it
# HEADER: is definitely not recommended.

The next two examples follow the same basic pattern, requiring that a host and IP address be specified, and write the same records, just with additional host aliases. In the third example you can see that in order to pass more than a single host_alias to a host entry you'll need to supply them as an array.

See also