Changed enc to output the original yaml file
This commit is contained in:
parent
b997a1681c
commit
a5f44aada3
47
enc.py
47
enc.py
@ -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:
|
||||||
|
print('Usage: {} <hostname>'.format(sys.argv[0]), file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
hostname = sys.argv[1]
|
||||||
|
|
||||||
# Get the hostname from command line argument
|
# Construct the file path
|
||||||
hostname = sys.argv[1]
|
file_path = '/opt/puppetlabs/enc/data/{}.yaml'.format(hostname)
|
||||||
|
|
||||||
# Construct the file path
|
# Try to open and read the YAML file
|
||||||
file_path = os.path.join(enc_data_dir, f'{hostname}.yaml')
|
try:
|
||||||
|
with open(file_path, 'r') as f:
|
||||||
|
file_content = f.read()
|
||||||
|
try:
|
||||||
|
# Parse the YAML file
|
||||||
|
yaml.safe_load(file_content)
|
||||||
|
# If correctly parsed, print the raw content
|
||||||
|
print(file_content)
|
||||||
|
except yaml.YAMLError as e:
|
||||||
|
print('Error in {}: {}'.format(file_path, e), file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
except IOError as e:
|
||||||
|
print('Could not open {}: {}'.format(file_path, e), file=sys.stderr)
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
# Check if the file exists
|
if __name__ == '__main__':
|
||||||
if os.path.exists(file_path):
|
main()
|
||||||
# If the file exists, open it and load the YAML data
|
|
||||||
with open(file_path, 'r') as file:
|
|
||||||
data = yaml.safe_load(file)
|
|
||||||
print(yaml.dump(data))
|
|
||||||
else:
|
|
||||||
# If the file does not exist, print an error message and exit
|
|
||||||
sys.stderr.write(f"Error: File {file_path} does not exist.\n")
|
|
||||||
sys.exit(1)
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user