๐ง Day 15 Task: Python Libraries for DevOps ๐ง
Table of contents
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! ๐๐๐