Boosting Your DevOps Arsenal: Must-Have Python Libraries for Seamless Operationsπ
Day -15 #90daysofdevopschallenge
Table of contents
- π Introduction
- π What is JSON?
- π What is YAML?
- π Task -1 Create a Dictionary in Python and write it to a JSON File.
- π Task -2 Read a json file services.json and print the service names of every cloud service provider
- π Task -3 Read YAML file using python, file services.yaml and read the contents to convert yaml to json
π Introduction
DevOps Engineers play a crucial role in managing software development and deployment. Their responsibilities often involve handling various file formats for configuration, data exchange, and automation. Python, being a versatile programming language, provides a suite of libraries such as os, sys, json, and yaml that are invaluable assets for DevOps tasks. These libraries empower engineers to efficiently navigate file systems, manipulate data, and seamlessly integrate applications. With the aid of these libraries, DevOps Engineers can streamline processes, enhance collaboration, and contribute to the success of development projects.
π What is JSON?
JSON (JavaScript Object Notation) is a lightweight data interchange format that's easy for humans to read and write, and easy for machines to parse and generate. It uses a simple structure of key-value pairs and arrays to represent data.
JSON is like a digital translator that helps different programs share information. Imagine it as a language that both humans and computers can understand. 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.
Key Points:
Format: JSON uses a combination of objects (key-value pairs) and arrays (ordered lists) to organize data.
Human-Readable: JSON is easy for humans to read and write since it follows a simple and clear structure.
Data Types: JSON supports various data types like strings, numbers, objects, arrays, booleans, and null values.
Widely Used: It's a common format for APIs, configurations, and data storage.
Example:
{ "name": "Deepak Nayak", "age": 21, "isStudent": false, "hobbies": ["reading", "gaming"] }
π What is YAML?
YAML (YAML Ain't Markup Language) is a human-readable data serialization format. It's commonly used for configuration files and data exchange between languages with different data structures.
π° Why YAML?
Readable: YAML uses indentation and plain text, making it easily readable and writable by both humans and machines.
Simplicity: Its simple syntax reduces complexity and makes writing configurations a breeze.
Versatility: YAML supports various data types like strings, numbers, lists, and dictionaries.
Configurations: YAML is a popular choice for configuration files in applications, simplifying setup.
Cross-Language: It's used across programming languages, making it great for interoperability.
π Basic Syntax Example:
person:
name: Deepak Nayak
age: 21
hobbies:
- reading
- Blog Writing
π 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)