Day 15 Task: Python Libraries for DevOps
Table of contents
Introduction
Welcome to Day 15 of our DevOps learning journey! Today, we will explore Python libraries that play a crucial role in managing configurations and data in DevOps environments. We will cover four tasks, including reading JSON and YAML in Python, creating a dictionary and writing it to a JSON file, reading a JSON file and extracting data, and converting YAML to JSON for easier readability. And just to make it a bit more fun, we'll sprinkle some emojis throughout the post! ๐
Task 1: Reading JSON and YAML in Python
In DevOps, you often need to work with configuration files in various formats. Two common formats are JSON and YAML. Python provides libraries to read both.
As a DevOps Engineer, you should be able to parse files, be it txt, json, yaml, etc.
You should know what all libraries one should use in Python for DevOps.
Python has numerous libraries like
os
,sys
,json
,yaml
etc that a DevOps Engineer uses in day-to-day tasks.
Let's start with JSON:
JSON stands for JavaScript Object Notation. JSON data is represented in key-value pairs, where the keys are strings, and the values can be of various data types, such as strings, numbers, booleans, arrays, and even other JSON objects. JSON values cannot be functions or methods, they are meant to be data only. JSON objects are enclosed in curly braces {}
, and each key-value pair is separated by a colon :
.
import json
#create a dictionary
data = {
"project": "DevOps",
"tasks": ["Automate Everything", "Continuous Integration", "Containerization"]
}
#define the filename for the json file
file_name = "cicd.json"
#write the dictionary to a json file
with open('file_nmae', 'w') as file:
obj = json.dump(data, file, indent=4)
print("Data written to cicd.json",obj)
For YAML, we can use the PyYAML
library:
import yaml
# Load YAML from a string
yaml_data = """
name: DevOps
task: Automate Everything
"""
data = yaml.safe_load(yaml_data)
print(f'YAML Data: {data}')
# Reading from a YAML file
with open('data.yaml', 'r') as file:
data_from_file = yaml.safe_load(file)
print(f'Data from YAML file: {data_from_file}')
Task 2: Create a Dictionary in Python and Write it to a JSON File
DevOps often involves managing configuration data. Let's create a dictionary and write it to a JSON file:
json.dumps() : It is used to convert a dictionary to JSON string.
Task 3: Read a json file services.json
kept in this folder and print the service names of every cloud service provider.
output
aws : ec2
azure : VM
gcp : compute engine
Let's say you have a JSON file called services.json
containing cloud service provider information. We want to read it and print the service names for each provider:
import json
# JSON data
json_data = '''
{
"services": {
"debug": "on",
"aws": {
"name": "EC2",
"type": "pay per hour",
"instances": 500,
"count": 500
},
"azure": {
"name": "VM",
"type": "pay per hour",
"instances": 500,
"count": 500
},
"gcp": {
"name": "Compute Engine",
"type": "pay per hour",
"instances": 500,
"count": 500
}
}
}
'''
# Parse JSON data
data = json.loads(json_data)
# Get and print service names
cloud_providers = ["aws", "azure", "gcp"]
for provider in cloud_providers:
service_name = data["services"][provider]["name"]
print(f"Cloud Provider: {provider.upper()}, Service Name: {service_name}")
Task 4: Converting YAML to JSON for Easier Readability
Sometimes, it's more convenient to work with configuration data in JSON format rather than YAML. We can convert YAML to JSON to achieve this:
pythonCopy codeimport yaml
import json
# Read YAML data from a file
with open('config.yaml', 'r') as file:
yaml_data = yaml.safe_load(file)
# Convert YAML to JSON
json_data = json.dumps(yaml_data, indent=4)
with open('config.json', 'w') as file:
file.write(json_data)
print("YAML data converted to JSON and saved in config.json")
Now, you have your YAML data transformed into a JSON format, making it more readable and easier to work with in DevOps processes. ๐
Conclusion
DevOps isn't just about automation; it's also about effective management of configurations and data. Python's libraries for working with JSON and YAML make these tasks more manageable, and the included emojis make learning DevOps a bit more fun! ๐
Stay tuned for more DevOps adventures on our learning journey! ๐จโ๐ป๐ฉโ๐ป๐