What is the best way in Puppet to add a piece of text to the /etc/hosts file?

Honestly, using the host resource is the simplest way to do this. You only have to define the hosts you want controlled by puppet, and you can still edit the rest of the file by hand (even though Puppet drops in that header that tells you not to).

The augeas module is overkill for a hosts file, because it just duplicates the functionality of the host resource (although it doesn't add in the "don't edit this file" header).

If you really want something more complicated or you want fine control over the placement of lines in the file, use the concat module with a local source for one of the fragments. There's an example for just that sort of thing (using the motd file) in the concat documentation.

But really, just use the host resource for the hosts you want to define from Puppet and edit the local hosts files for anything else you need.

Also note that you can write the host definitions pretty compactly in Puppet:

host {
  # Public IPs - eth0
  'front-01': ip => '192.168.1.103';
  'front-02': ip => '192.168.1.106';

  # Private IPs - eth1
  'priv0-0': ip => '192.169.40.201';
  'priv0-1': ip => '192.169.40.202';
  'priv1-0': ip => '192.169.40.207';
  'priv1-1': ip => '192.169.40.208';

  # Virtual IPs - eth0:1
  'vip-01': ip => '192.169.50.202';
  'vip-02': ip => '192.169.50.205';
}

Tags:

Puppet