Migrating away from Puppet’s deprecated import feature

The import keyword in Puppet has been deprecated and will be removed in Puppet v4. That’s good to know, but what can you do about it if you’re using it? Let’s take a look at how it might be used. All directories below are relative to your environment directories, such as /etc/puppet/environments/production, unless a full path is given (starts with /).

Current Setup

Here’s what your site manifest might look like:

#manifests/site.pp
import nodes/*pp

When Puppet starts, it looks at all the *pp files in the nodes directory and loads them. Those files might look like this, one for each node or node-class:

#manifests/nodes/puppet.pp
node /puppet/ {
  include role::puppet
}
#manifest/nodes/www.pp
node /www/ {
  include role::www
}

Future Setup

You want to get rid of import, but you need the ability to migrate away from your current setup. If you only have a few node manifests, maybe you can do a forklift upgrade, but if you have dozens or hundreds or thousands, you are going to want to move a little slower so you can test as you go. There are a number of ways forward, but the simple solution I’ve chosen only requires you to have hiera configured and enabled. If you need to, go do that now and then come back. We’ll wait.

Here’s a simple hiera configuration that looks very much like the out-of-the-box setup:

#/etc/hiera.yaml (symlink to /etc/puppet/hiera.yaml)
---
:backends:
  - yaml
:hierarchy:
  - defaults
  - clientcert/%{clientcert}
  - environment/%{environment}
  - global

:yaml:
# datadir is empty here, so hiera uses its defaults:
# - /var/lib/hiera on *nix
# - %CommonAppData%\PuppetLabs\hiera\var on Windows
# When specifying a datadir, make sure the directory exists.
  :datadir: /etc/puppet/hiera/

With this in place, modify your site manifest slightly:

#manifests/site.pp
import nodes/*pp
# This takes care of other deprecation statements
Package {
  allow_virtual => true,
}
node default { 
  hiera_include('classes')
}

What we have done here is to still import the node definitions, but for any node without a more specific definition, we’ve created a default entry. For these nodes, hiera will be examined for the classes values. Each of those values will be included, i.e. include value1 and include value2, etc. You can now convert your node definitions one at a time. Delete manifests/nodes/puppet.pp and create the following file.

#/etc/puppet/hiera/clientcert/puppet.company.com.yaml
---
classes:
  role::puppet

This is the equivalent of the node definition in the manifest file. We can do the same for manifests/nodes/www.pp:

#/etc/puppet/hiera/clientcert/www.company.com.yaml
---
classes:
  role::www

You can work through your remaining node entries, deleting and converting to hiera definitions (I have a few hiera articles!). Once all node definitions are migrated, update your site manifest one more time:

#manifests/site.pp
# This takes care of other deprecation statements
Package {
  allow_virtual => true,
}
node default {
  hiera_include('classes')
}

Voila! No more deprecation warnings and you’re ready for Puppet 4!

Leave a comment