8

I would also like to keep the ability to manually edit hosts-file, at least the first 10 lines.

#Public IP's - eth0
192.168.1.103   front-01
192.168.1.106   front-02

#Private IP's - eth1
192.169.40.201  priv0-0
192.169.40.202  priv0-1
192.169.40.207  priv1-0
192.169.40.208  priv1-1

#Virtual IP's - eth0:1
192.169.50.202  vip-01
192.169.50.205  vip-02

Having these hosts entries at the bottom of the /etc/hosts, would be perfect. What is the best way to do this? Is there a better way than writing 8 hosts-lines manifest?

# create a simple hostname and ip host entry
host { 'front-01':
    ip => '192.168.1.103',
}

There might be server groups that need different IP's /hostnames in their /etc/hosts. I would use a template, but that means people can no longer make manual changes in their /etc/hosts as they would get overwritten by the template.

ujjain
  • 3,963
  • 15
  • 50
  • 88

4 Answers4

14

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';
}
asciiphil
  • 3,036
  • 3
  • 26
  • 52
1

Use the augeaus functionality built into puppet. You want something like

augeas {
  context => "files/etc/hosts"
  changes => [
    "set <ip address> <name>",
  ],
}
Cian
  • 5,777
  • 1
  • 27
  • 40
0

One case where you may choose to use augeas for the /etc/hosts file, in addition to hosts resource types -- is for duplicate IP address lines.

hosts resource type quite happily allows lines with the same IP addresses. (Google it; where are extremely obscure cases when it may be a good idea.)

But if you don't want duplicate IP addresses in your /etc/hosts file -- then augeas can help you. hosts is no help for that.

spike
  • 1
  • 1
-1

Just a method, without code:

Use templating or whatever that updates a file /etc/hosts.puppet with clear start and end lines

###PUPPETSTART###
## Users: Lines between START and END will be deleted, put your own entries below 

192.168.0.1 host1
###PUPPETEND###

and have a shell script as dependency that cuts this segment out of /etc/hosts and replaces it with the content of /etc/hosts.puppet.

Beware that this has a race condition when users edit the file and the puppet run changes it. Alternatively, instruct your users to edit a file /etc/hosts.user and construct /etc/hosts out of both files whenever one changes.

Sven
  • 97,248
  • 13
  • 177
  • 225