๐Ÿ”ง Day 15 Task: Python Libraries for DevOps ๐Ÿ”ง

ยท

3 min read

Table of contents

No heading

No headings in the article.

Hey there, fellow DevOps Engineers! ๐Ÿš€ Today, we'll dive into the world of Python libraries that make our lives easier when it comes to parsing files like txt, json, and yaml. ๐Ÿ“‚ Let's get started!

๐Ÿ“ Reading JSON and YAML in Python ๐Ÿ“

As DevOps Engineers, we often encounter scenarios where we need to handle configuration files, and two popular formats are JSON and YAML. Python provides us with a wealth of libraries to work with these file formats.

๐Ÿ“˜ JSON in Python ๐Ÿ“˜

JSON (JavaScript Object Notation) is widely used for data interchange. Python has a built-in library called 'json' ๐Ÿ that makes working with JSON a breeze.

๐Ÿ“‹ Task 1: Creating a Dictionary and Writing it to a JSON File ๐Ÿ“‹

First things first, let's create a dictionary in Python and write it to a JSON file:

import json

# Create a sample dictionary
data = {
    "name": "John Doe",
    "occupation": "DevOps Engineer",
    "experience": "5 years",
    "skills": ["Python", "Docker", "Kubernetes"],
}

# Write the dictionary to a JSON file
with open("devops_engineer.json", "w") as file:
    json.dump(data, file)

Congratulations! You've successfully created a dictionary and stored it in a JSON file named 'devops_engineer.json'. ๐ŸŽ‰

๐Ÿ“˜ Reading JSON Files in Python ๐Ÿ“˜

Now, let's move on to the next task.

๐Ÿ“‹ Task 2: Reading JSON File and Printing Cloud Service Providers ๐Ÿ“‹

Assuming you have a file named 'services.json' in the same folder, with the following content:

{
    "aws": "ec2",
    "azure": "VM",
    "gcp": "compute engine"
}

We can use the 'json' library to read the file and print the service names for each cloud provider:

# Read and print service names of every cloud service provider
with open("services.json", "r") as file:
    services = json.load(file)

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

๐ŸŽ‰ Ta-da! You've successfully read the JSON file and printed the service names for each cloud service provider. AWS ๐ŸŒฉ๏ธ offers 'ec2', Azure โ˜๏ธ has 'VM', and GCP ๐ŸŒ provides 'compute engine'.

๐Ÿ“— YAML in Python ๐Ÿ“—

YAML (YAML Ain't Markup Language) is another popular data serialization format used in configuration files. To work with YAML in Python, we'll use the 'PyYAML' library ๐Ÿ.

๐Ÿ“‹ Task 3: Reading YAML File and Converting it to JSON ๐Ÿ“‹

Assuming you have a file named 'services.yaml' in the same folder, with the following content:

aws: ec2
azure: VM
gcp: compute engine

We can use the 'PyYAML' library to read the YAML file and convert it to JSON:

import yaml

# Read YAML file and convert to JSON
with open("services.yaml", "r") as file:
    data = yaml.safe_load(file)

# Convert YAML to JSON
json_data = json.dumps(data)

print(json_data)

๐ŸŽ‰ Fantastic! You've successfully read the YAML file and converted it to JSON format.

๐Ÿš€ Wrapping Up ๐Ÿš€

Today, we explored essential Python libraries for DevOps Engineers, including 'json' for JSON handling and 'PyYAML' for YAML processing. Armed with these tools, you're well-equipped to tackle various file parsing tasks in your day-to-day work. Happy coding! ๐Ÿ˜Š๐Ÿ‘จโ€๐Ÿ’ป๐Ÿ‘ฉโ€๐Ÿ’ป

Stay curious, keep learning, and see you on the next task! ๐Ÿ‘‹๐Ÿ‘‹๐Ÿ‘‹

ย