Skip to content

Instantly share code, notes, and snippets.

@ptomulik
Last active August 29, 2015 13:56
Show Gist options
  • Save ptomulik/8999349 to your computer and use it in GitHub Desktop.
Save ptomulik/8999349 to your computer and use it in GitHub Desktop.
Simple node clasifier for Puppet written in python.
#! /usr/bin/env python
# Python ENC
# receives fqdn as argument
import yaml
import sys
enc = {
'classes' : { 'base' : {} },
'environment' : 'production',
'parameters' : { }
}
yaml.dump(enc, explicit_start = True, stream = sys.stdout)
#! /usr/bin/env python
# Python ENC
# receives fqdn as argument
import yaml
import sys
import re
try:
hostname = sys.argv[1].lower()
except IndexError:
# need a hostname
sys.exit(10)
m = re.match(r'^([a-z]+)-([a-z]+)-([a-z])([a-z])([0-9]+)$', hostname)
if m is None:
enc = {
'classes' : {
'base' : {},
'hostname_problem' : { 'enc_hostname' : hostname }
}
}
else:
role, location, os, environment, instance = m.groups()
enc = {
'classes' : {
'base' : {},
'role' : {}
},
'environment' : {
# map environment from hostname into environment
'p' : 'production',
'n' : 'nonprod',
'd' : 'devel',
's' : 'sbx'
}.get(environment, 'undef'),
# set top scope variables
'parameters' : {
'enc_hostname' : hostname,
'role' : role,
'location' : location,
'os' : os,
'instance' : instance
}
}
yaml.dump(enc, explicit_start = True, stream = sys.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment