Answers about Puppet

DevOps, Automation

Universe and Everything

Need Puppet help?

Contact Alessandro Franceschi / example42
for direct expert help on Puppet.
If solution is quick, it’s free. No obligations.

Tip of the Week 103 - Psick profiles. Part 5 - Managing /etc/hosts and DNS

Our tour of the ready to use profiles of the psick module continues this week with management of DNS entries for the resolver, and of the content of /etc/hosts file.

These are the previous posts of this series:

Managing /etc/hosts

Psick module provides 3 different classes to manage the contents of /etc/hosts:

  • psick::hosts::file: Manage /etc/hosts via a file resource

  • psick::hosts::dynamic: Manage /etc/hosts dynamically using exported resources

  • psick::hosts::resource: Manage /etc/hosts content via Puppet native host resource type

Using psick::hosts::file

This class just manages /etc/hosts as a file, you can customise the template to use for the content of this file and an array of entries to add to it.

To use this approach, just classify your nodes with with profile:

include psick::hosts::file

Then it can be configured via Hiera with the following settings:

The erb template to use to manage the content of /etc/hosts (default value is psick/hosts/file/hosts.erb):

psick::hosts::file::template: profile/hosts/hosts.erb

The ip address (default is the value of $::psick::primary_ip which defaults to the fact $::networking['ip']), the short hostname (defaults to fact $::hostname) and the domain (defaults to fact $::domain) to use to identify the local node in /etc/hosts:

psick::hosts::file::hostname: my_server

An array of custom extra lines to add to /etc/hosts (default: []), each element of the array should contain the expected text in each extra line:

-	puppet
-	other_server

Using psick::hosts::dynamic

This class manages /etc/hosts automatically and dynamically: each server managed by Puppet exports its own host entry (via Puppet host resource) and collects the ones of all the other nodes.

The class provides options to customise IP and alias to export for a node, if to actually export the host entry and a special “magic var” which can be used to divide nodes in different groups (within each group hosts’ entries are exported and collected).

This class is alternative to the others, it requires Store configs enabled on the Puppet Server (so, consequently, the usage of PuppetDB in not too ancient setups) and can be used with a simple:

include psick::hosts::dynamic

In small setups this could be enough to have /etc/hosts automatically managed with all the entries of all the nodes.

It’s possible anyway to customise some entries. For example the ip address (default $::ipaddress) and the array of aliases (default [ $::hostname ] to use when exporting the local host’s info):

  - my_server
  - my_server_other_alias

It’s also possible to control if and how to export and collect the node’s host entry.

It’s possible to set a string that allows to group together nodes: all nodes having this magic var set collect and export host resources only for nodes using the same magic var:

psick::hosts::dynamic::dynamic_magicvar: intranet

If we don’t want to collect in any place the host entry of a node we can set, for it (default value is false, so each node exports a valid and collectable host resource):

psick::hosts::dynamic::dynamic_exclude: true

Since /etc/hosts entries are managed via exported host Puppet resources, it’s also possible to specify an Hash of custom additional entries to add to the host file. This makes sense if we want to add references to hosts or devices not managed by Puppet. The syntax of the hash to use maps the available arguments of the host resource (puppet describe host for a full list):

    target: /etc/hosts # (Default)
      - firewall
      - san

Using psick::hosts::resource

This is the third alternative to manage /etc/hosts. It’s just a wrapper that exposes an Hiera controllable entry point for Puppet host resource. This is alternative to the previous profiles and has to be classified as the others:

include psick::hosts::resource

Configuration the is done via an Hash of hosts resources similar to what we have seen for psick::hosts::dynamic::extra_hosts:

    target: /etc/hosts # (Default)
      - firewall
      - san

Managing DNS resolver.

To manage the contents of /etc/resolver you can use the psick::dns::resolver profile. Classify it with something equivalent to :

include psick::dns::resolver

And then configure on Hiera both the template to use and its entries.

To manage the template to use (default is ‘psick/dns/resolver/resolv.conf.erb’) and the actual path of the resolver file (default is ‘/etc/resolv.conf’):

psick::dns::resolver::resolver_path: /etc/resolv.conf
psick::dns::resolver::resolver_template: profile/resolver/resolv.conf.erb

To manage the actual typical configuration settings of resolv.conf:

  - attempts: 2
  - timeout: 2
  - rotate

That’s all for today. As we have seen, for the most common use cases, on Linux systems, Psick provides ready to use profiles to manage the content of /etc/hosts and /etc/resolv.conf without the need of fetching dedicated modules.

Have fun with Puppet, Life, Universe and Everything.

Alessandro Franceschi