Python Libraries for DevOps

ยท

2 min read

Excited to share the next step of my #90daysofdevops Journey again!

Over the past two days, I immersed myself in Python and can't control my thirst for learning that. I want to show some of the tasks I have completed while retaining the same.

Task 1: Create a Dictionary and Write to JSON File

import json

# Create a dictionary
cloud_services = {
    "aws": "ec2",
    "azure": "VM",
    "gcp": "compute engine"
}

# Write the dictionary to a JSON file
with open('services.json', 'w') as json_file:
    json.dump(cloud_services, json_file, indent=4)

This code will create a services.json file in the current folder and write the cloud_services dictionary into it.

Task 2: Read JSON File and Print Service Names

# Read the JSON file and print the service names
with open('services.json', 'r') as json_file:
    data = json.load(json_file)

for provider, service in data.items():
    print(f"{provider} : {service}")

Now, let's read a YAML file, convert it to JSON, and then print the contents.

Task 3: Read YAML File and Convert it to JSON

To perform this step, you'll need to install the PyYAML library, which can be done using pip install PyYAML. Make sure to install it before proceeding.

import yaml

# Read the YAML file and convert to JSON
with open('services.yaml', 'r') as yaml_file:
    yaml_data = yaml.safe_load(yaml_file)

# Convert to JSON
json_data = json.dumps(yaml_data, indent=4)

# Print the JSON data
print(json_data)

Now, we have successfully read the services.yaml file converted its contents to JSON, and printed the JSON data.

Remember to place the services.json and services.yaml files in the same folder as your Python script to ensure they are correctly read and written.

And here we come to the end of day 15 in our #90daysofdevops challenge. Stay tuned for more exciting updates.๐ŸŽ‰

#PythonBasics #DevOpsLearning #ContinuousImprovement #TechJourney #PythonProgramming #LearningInProgress #devopscommunity

ย