Skip to main content

Command Palette

Search for a command to run...

Day 15 : Python Libraries for DevOps

Updated
β€’3 min read
Y

I am Yashraj Singh Sisodiya, a 3rd Year CSE student at SVVV, born and raised in Shujalpur. Currently residing in Indore, I'm passionate about pursuing a career in DevOps engineering. My tech journey began with an internship at Infobyte, honing my skills as an Android Developer intern. Alongside my academic pursuits, I actively participate in co-curriculars, holding roles as Technical Lead at Abhyudaya and Cloud Lead at GDSC SVVV, while also serving as an MLSA of my college.

I have a keen interest in Cloud Computing, demonstrated through projects such as User management and Backup using shell scripting Linux, Dockerizing applications, CI/CD with Jenkins, and deploying a 3-tier application on AWS. Always eager to learn, I'm committed to expanding my knowledge and skills in the ever-evolving tech landscape.

Reading JSON and YAML in Python:

JSON (JavaScript Object Notation) and YAML (YAML Ain't Markup Language) are both popular formats for storing and transmitting structured data. They are commonly used in configuration files, API responses, and data interchange between systems. Here's a brief explanation of each with an example:

JSON

JSON is a lightweight data interchange format inspired by JavaScript object literal syntax. It's easy for both humans and machines to read and write. JSON data is represented as key-value pairs, where keys are strings and values can be strings, numbers, arrays, objects, booleans, or null.

Example of JSON:

{
  "name": "John Doe",
  "age": 30,
  "city": "New York",
  "is_student": false,
  "hobbies": ["reading", "hiking", "cooking"],
  "address": {
    "street": "123 Main St",
    "zipcode": "10001"
  }
}

YAML

YAML is a human-readable data serialization format that aims to be easily readable by humans and easily parseable by machines. It uses indentation to represent data structures and relies on whitespace indentation to denote nesting. YAML supports complex data structures such as arrays, dictionaries, and nested objects.

Example of YAML:

name: Yashraj singh sisodiya
age: 22
city: indore
is_student: false
hobbies:
  - reading
  - hiking
  - cooking
address:
  street: sundar nagar extension
  zipcode: '456010'

In Python, you can use the json and yaml libraries to work with JSON and YAML data, respectively. Here's a simple example of reading JSON and YAML files in Python:

import json
import yaml

# Read JSON file
with open('data.json') as json_file:
    json_data = json.load(json_file)
    print("JSON Data:")
    print(json_data)

# Read YAML file
with open('data.yaml') as yaml_file:
    yaml_data = yaml.safe_load(yaml_file)
    print("\nYAML Data:")
    print(yaml_data)

This code snippet demonstrates how to read JSON and YAML files using Python's built-in libraries (json for JSON and yaml for YAML). The data from both files is loaded into Python dictionaries for further processing.

Tasks:

Here are three code snippets, each with comments:

parser.py

This script parses both JSON and YAML files and prints their contents:

import json
import yaml

# Define file paths
json_file = "services.json"
yaml_file = "services.yaml"

# Read and parse JSON file
with open(json_file, 'r', encoding='utf-8') as f:
    json_data = json.loads(f.read())

print("JSON:\n", json_data)

# Read and parse YAML file
with open(yaml_file, "r") as stream:
    try:
        yaml_data = yaml.safe_load(stream)
    except yaml.YAMLError as exc:
        print(exc)

JSON

This is the 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
        }
    }
}

YAML

This is the YAML equivalent of the 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

These scripts and data represent a scenario where you're reading service information from both JSON and YAML files. The parser.py script is responsible for parsing both types of files, while the JSON and YAML snippets represent the content of the respective files.

More from this blog

Mastering DevOps: A Comprehensive Guide to Becoming a DevOps Engineer

39 posts

πŸš€ Welcome to the world of DevOps with Yashraj! 🌐

Hey tech enthusiasts! πŸ‘‹ I'm Yashraj, a dedicated third-year student pursuing Computer Science Engineering at SVVV Indore. Currently, I'm honing my πŸš€ Welcome to the world of DevOps with Yashraj! 🌐

Hey tech enthusiasts! πŸ‘‹ I'm Yashraj, a dedicated third-year student pursuing Computer Science Engineering at SVVV Indore. Currently, I'm honing my skills in the realm of DevOps, fueled by my passion for technology and innovation.

πŸ› οΈ My Tech Arsenal:

  • Docker
  • Linux
  • Kubernetes
  • Jenkins
  • AWS
  • Python
  • Java
  • Prometheus
  • Grafana
  • Ansible
  • Terraform

Join me in exploring the fascinating landscape of AWS DevOps technology. Let's embark on a journey of continuous learning and growth, embracing new challenges with enthusiasm and determination!

🀝 Connect with me for:

  • Engaging discussions
  • Shared learning experiences
  • Collaborative learning opportunities

Follow my journey on Twitter and LinkedIn for daily insights and tech updates. Together, let's harness the power of DevOps to build, automate, and innovate our way to success! πŸš€ #DevOps #AWS #TechEnthusiast #InnovationJourney #90DaysOfDevOps