Changed enc to output the original yaml file

This commit is contained in:
Ben Vincent 2023-06-18 14:53:41 +10:00
parent b997a1681c
commit a5f44aada3

43
enc.py
View File

@ -1,25 +1,32 @@
#!/usr/bin/env python3 #!/usr/bin/env python
import yaml
import os
import sys import sys
import yaml
# Path to your ENC data directory def main():
enc_data_dir = '/opt/puppetlabs/enc/data' # Get the hostname from the command line arguments
if len(sys.argv) != 2:
# Get the hostname from command line argument print('Usage: {} <hostname>'.format(sys.argv[0]), file=sys.stderr)
sys.exit(1)
hostname = sys.argv[1] hostname = sys.argv[1]
# Construct the file path # Construct the file path
file_path = os.path.join(enc_data_dir, f'{hostname}.yaml') file_path = '/opt/puppetlabs/enc/data/{}.yaml'.format(hostname)
# Check if the file exists # Try to open and read the YAML file
if os.path.exists(file_path): try:
# If the file exists, open it and load the YAML data with open(file_path, 'r') as f:
with open(file_path, 'r') as file: file_content = f.read()
data = yaml.safe_load(file) try:
print(yaml.dump(data)) # Parse the YAML file
else: yaml.safe_load(file_content)
# If the file does not exist, print an error message and exit # If correctly parsed, print the raw content
sys.stderr.write(f"Error: File {file_path} does not exist.\n") print(file_content)
except yaml.YAMLError as e:
print('Error in {}: {}'.format(file_path, e), file=sys.stderr)
sys.exit(1) sys.exit(1)
except IOError as e:
print('Could not open {}: {}'.format(file_path, e), file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()