In this article we’ll dive deeper, and look at how to use Landscape as an External Node Classifier for Puppet.

In a typical Puppet configuration, all your nodes are defined in site.pp under the “node” definitions section. Each node is assigned Puppet classes by manually editing the file. When your number of nodes grows, manually managing the site.pp file becomes tedious and error prone.

An External Node Classifier (ENC) is Puppet’s way of offloading the tedious node maintenance to an external program. The interface is dead simple – puppet executes the external node classifier program with a single full node name as the only argument. The classifier just has to write a YAML blob out to stdout before exiting.

To start, let’s create a simple python ENC in /etc/puppet/landscape_enc – Don’t forget to make the file executable.

   1 #!/usr/bin/env python
   2 import sys
   3 import yaml
   5 # The node name to be classified is supplied as an argument to the script
   6 node_name = sys.argv[1]
   8 classes = ["basenode"]
  10 # Output must be a YAML document
  11 print(yaml.dump({
  12     "classes": classes,
  13     }))

It ignores the node name and just puts everything into the “basenode” class. Not very interesting but it’s enough to get started with Puppet.

NOTE: These examples are all using puppet 2.7 which ships on Ubuntu 12.04 Precise LTS. The ENC functionality behaves a bit differently in versions of puppet earlier than 2.65 – See for details.

To test the ENC I put together a minimal puppet configuration with two simple classes and put everything into my site.pp in /etc/puppet/manifests/site.pp

class basenode {
  notify {"I am a basenode!":}

class specialnode {
  notify {"I am a specialnode!":}

Notice that no nodes are actually defined. That is the ENC’s job. To enable the ENC you need to add two lines to your puppetmaster’s config file /etc/puppet/puppet.conf

Add these lines at the end of the “[master]” section:

node_terminus = exec
external_nodes = /etc/puppet/landscape_enc

You can now test that the puppetmaster is using your new, rather basic, ENC.

ubuntu@ubuntu:~$ sudo puppet agent --test

info: Caching catalog for ubuntu
info: Applying configuration version '1354824718'
notice: I am a basenode!
notice: /Stage[main]/Basenode/Notify[I am a basenode!]/message: defined 'message' as 'I am a basenode!'
notice: Finished catalog run in 0.06 seconds

As you can see, with our trivial ENC everyone is a basenode.

Now we’re going to enhance our ENC to ask Landscape to classify nodes for us.

The Landscape API To use the Landscape API you need three pieces of information: the Landscape API endpoint URI, the user key, and the secret for your user.

To find your API credentials, log in to the Landscape web interface and click your username on the top right. Your API credentials are in the “API Access” section.

For this example, we’ll use the python API client provided with Landscape (NOTE, you must install the landscape-api package first). Here’s how to query for a computer registered with Landscape using it’s host name:

   1 from landscape_api.base import API
   3 landscape_uri = ""
   4 landscape_key = "43NW6OV71L32CSOPCJGX"
   5 landscape_secret = "agBf3v267DqO8vtVRnzjseWfYdV4ueklj5a81iIT"
   6 api = API(landscape_uri, landscape_key, landscape_secret)
   8 api.get_computers(query="my_hostname_here")
   9 [{u'access_group': u'server',
  10 u'comment': u'',
  11 u'hostname': u'appserv1',
  12 u'id': 1,
  13 u'last_exchange_time': None,
  14 u'last_ping_time': u'2012-09-07T15:19:22Z',
  15 u'reboot_required_flag': False,
  16 u'tags': [u'lucid', u'server', u'puppet-webfarm'],
  17 u'title': u'Application Server 1',
  18 u'total_memory': 1024,
  19 u'total_swap': 1024}]

   1 #!/usr/bin/env python
   2 import sys
   3 import yaml
   4 from landscape_api.base import API
   6 # Create our connection to Landscape
   7 landscape_uri = ""
   8 landscape_key = "43NW6OV71L32CSOPCJGX"
   9 landscape_secret = "agBf3v267DqO8vtVRnzjseWfYdV4ueklj5a81iIT"
  10 api = API(landscape_uri, landscape_key, landscape_secret)
  12 # The node name to be classified is supplied as an argument to the script
  13 node_name = sys.argv[1]
  15 # Ask Landscape about the computer
  16 computers = api.get_computers(query=node_name)
  18 # If we don't get back any computers or if we get more than one, error out.
  19 # You could also handle this case by simply giving the node a default class.
  20 if len(computers) != 1:
  21     sys.stderr.write("Only expecting one computer, instead got this: %s" % computers)
  22     sys.exit(1)
  24 # Extract the tags from our computer
  25 tags = computer[0]["tags"]
  27 # Now here you can use whatever logic you want to convert
  28 # tags into classes. I'm going to use any tag that starts with "puppet-"
  29 # as a class name. I'm also going to make sure every node gets the
  30 # "basenode" class
  31 classes = ["basenode"]
  32 for tag in tags:
  33     if tag.startswith("puppet-"):
  34         class_name = tag.split("-",1)[1]
  35         classes.append(class_name)
  37 # Output must be a YAML document
  38 print(yaml.dump({
  39     "classes": classes,
  40     }))

That’s all there is to it. Now if you tag a computer “puppet-database” in Landscape, it will automatically get the “database” class in Puppet.

You can see in the script comments that it’s very easy to customize the behaviour to match your environment. Now tag away and let Landscape and Puppet take over your world!

PuppetIntegration (last edited 2017-12-14 10:23:30 by adam-collard)