puppet-enc/enc.py

47 lines
1.3 KiB
Python
Executable File

#!/usr/bin/env python
"""
External Node Classifier (ENC) for Puppet.
It reads in YAML files located at '/opt/puppetlabs/enc/data'.
If the environment specified in the YAML file is 'testing',
the environment is not included in the output.
"""
import sys
import yaml
def main():
# Get the hostname from the command line arguments
if len(sys.argv) != 2:
print("Usage: {} <hostname>".format(sys.argv[0]), file=sys.stderr)
sys.exit(1)
hostname = sys.argv[1]
# Construct the file path
file_path = "/opt/puppetlabs/enc/data/{}.yaml".format(hostname)
# Try to open and read the YAML file
try:
with open(file_path, "r") as f:
data = yaml.safe_load(f)
# If the environment is 'testing', remove it from the output
if data.get("environment") == "testing":
data.pop("environment", None)
# Print the YAML without the 'environment' if it was 'testing'
print(yaml.dump(data))
except IOError as e:
print("Could not open {}: {}".format(file_path, e), file=sys.stderr)
sys.exit(1)
except yaml.YAMLError as e:
print("Error in {}: {}".format(file_path, e), file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()