Installing/Updating /etc/hosts file using hosts Puppet Resource

Puppet comes with a builtin hosts resource which installs and manages host entries. More of this resource is available at
https://docs.puppetlabs.com/puppet/latest/reference/types/host.html

A host resource will look like this
 

host { 'resource title':
 name      => # (namevar) The host...
 ensure    => # The basic property that the resource should be..
 comment   => # A comment that will be attached to the line with
 host_aliases => # Any aliases the host might have.  Multiple...
 ip          => # The host's IP address, IPv4 or...
 provider    => # The specific backend to use for this `host...
 target      => #The file in which to store service information. 
  # ...plus any applicable metaparameters.
}



An example of hosts resource is as shown below



node 'node2.example.com','node3.example.com'{
host {
        'localhost.localdomain':
        ip => '127.0.0.1',
        host_aliases => 'localhost',
        ensure => 'present',
     }
host {
        'master.example.com':
        ip => '10.134.39.245',
        host_aliases => 'master',
        ensure => 'present',
     }
host {
        'node2.example.com':
        ip => '10.134.39.246',
        host_aliases => 'node2',
        ensure => 'present',
     }
host {
        'node3.example.com':
        ip => '10.134.39.247',
        host_aliases => 'node3',
        ensure => 'present',
     }
    }

 

This will create hosts entry as shown below


# cat /etc/hosts
# HEADER: This file was autogenerated at Tue Mar 08 12:42:36 +0530 2016
# HEADER: by puppet.  While it can still be managed manually, it
# HEADER: is definitely not recommended.
127.0.0.1       localhost.localdomain   localhost
10.134.39.245   master.example.com      master
10.134.39.246   node2.example.com       node2
10.134.39.247   node3.example.com       node3




There is some significance of "ensure" parameter. When
"ensure" is set to present e.g ensure => 'present', if the entry is removed from the target host , then during the next run of puppet agent on the target host, the host entry will be added back . Similarly if ensure => 'false',

then the corresponding host entry will be removed from target host. If there is some requirement to add few more hosts entries on a particular host , then we can add the entries manually without needing to update the hosts resource in Puppet. Since this new host entry is not managed by puppet, it will persists in /etc/hosts file without being removed by puppet.  

Comments

Popular Posts