blob: 7fc096ff1a7cca427a38b51cbd73e855ce4d6642 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
#!/usr/bin/env python3
import yaml
import sys
import configparser
# https://puppet.com/docs/puppet/7/nodes_external.html
def main(args):
if len(args) == 1:
print('usage ./node-classifier <node-name>')
return
parser = configparser.ConfigParser()
conf_file = '/etc/node-classifier.ini'
if parser.read(conf_file) != [conf_file]:
print(f'Configuration file missing, expected {conf_file}')
return 1
try:
fmt = parser.get('common', 'node_fmt')
if fmt != 'yaml':
print(f'Unknown format {fmt}')
return 1
filename = parser.get('common', 'nodes')
except configparser.NoSectionError as e:
print(e)
return 1
except NoOptionError as e:
print(e)
return 1
with open(filename) as f:
data = yaml.load(f)
nodename = sys.argv[1]
instance = data.get(nodename)
if not instance:
instance = data.get('default')
print(yaml.dump(instance))
if __name__ == '__main__':
sys.exit(main(sys.argv))
|