Python Libraries for DevOps

Python Libraries for DevOps

Day -15 #90daysofdevopschallenge

Β·

2 min read

πŸ“Œ Introduction

As a DevOps Engineer, you should be able to parse files, be it txt, json, yaml, etc. Python has numerous libraries like os, sys, json, yaml etc that a DevOps Engineer uses in day-to-day tasks.

πŸ“Œ What is JSON?

JSON is a lightweight data interchange format used for storing and exchanging data. It's easy for humans to read and write, and it's also easy for machines to parse and generate. JSON data is organized into key-value pairs. The keys are strings, and values can be strings, numbers, booleans, arrays, or nested objects.

πŸ“Œ What is YAML?

YAML (YAML Ain't Markup Language) is a human-readable data serialization format. YAML uses indentation to represent the hierarchy and structure of data. No braces or brackets are used, making it visually clean and easy to read.

πŸ“Œ Task -1 Create a Dictionary in Python and write it to a json File.

#! /usr/bin/python3

import json

dict ={"Number_first":"1","Number_second":"2","Number_third":"3"}

print("This is the dictionary - ",dict)

print(type(dict))

json = json.dumps(dict, indent=3)

print(json)

πŸ“Œ Task -2 Read a json file services.json and print the service names of every cloud service provider

#! /usr/bin/python3

import json

file = open('services.json')
data = json.load(file)

print(data)

print("aws: ", data["services"] ["aws"] ["name"])
print("azure: ", data["services"] ["azure"] ["name"])
print("gcp: ", data["services"] ["gcp"] ["name"])

πŸ“Œ Task -3 Read YAML file using python, file services.yaml and read the contents to convert yaml to json


#! /usr/bin/python3

import yaml
import json

file=open('services.yaml')
data= yaml.full_load(file)

print(data)
print("=============== json file =================")
json_file= json.dumps(data, indent=4)
print(json_file)

Β