Skip to main content

Python Libraries for DevOps| Day 15 of 90 Days of DevOps

Updated 2 min readMarkdown
Part of series90DaysOfDevOps
Python Libraries for DevOps| Day 15 of 90 Days of DevOps

🚀 Welcome to Day 13 of #90DaysOfDevOps! 🎉

Today, let's dive into a crucial aspect of being a DevOps Engineer - reading JSON and YAML in Python. 🐍📚

📝 Reading JSON and YAML in Python As DevOps Engineers, parsing files is a fundamental skill. Python offers powerful libraries that simplify handling various file formats like txt, json, and yaml.

Libraries for DevOps in Python Python boasts a wealth of libraries like os, sys, json, and yaml, which prove indispensable in our day-to-day tasks.

  • Task 1: Create a Dictionary and Write to JSON Let's start by creating a Python dictionary and writing it to a JSON file. JSON provides an organized and human-readable structure for data exchange.

      import json
    
      # Create a Python dictionary
      data = {
          "name": "John Doe",
          "age": 30,
          "occupation": "DevOps Engineer"
      }
    
      # Write dictionary to JSON file
      with open("data.json", "w") as json_file:
          json.dump(data, json_file, indent=4)
    

  • Task 2: Read JSON and Extract Cloud Service Names Now, we'll read a JSON file, "services.json," and extract the service names of each cloud service provider. The content of "services.json" is shown below:

      {
          "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
              }
          }
      }
    
      # Read JSON and extract cloud service names
      with open("services.json", "r") as json_file:
          data = json.load(json_file)
    
      cloud_providers = data["services"].keys()
      print("Cloud Service Providers:")
      for provider in cloud_providers:
          if provider != "debug":
              print("- ", provider)
    

  • Task 3: Read YAML and Convert to JSON YAML is another popular data serialization format. Let's read "services.yaml" and convert its contents to JSON. The content of "services.yaml" is shown below:

      ---
      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
    
      import yaml
    
      # Read YAML and convert to JSON
      with open("services.yaml", "r") as yaml_file:
          data = yaml.safe_load(yaml_file)
    
      with open("services_converted.json", "w") as json_file:
          json.dump(data, json_file, indent=4)
    

🏁 Conclusion: Reading JSON and YAML files in Python is a fundamental skill for DevOps Engineers. With Python's powerful libraries, we can efficiently handle data and extract valuable information. Make sure to practice these tasks to enhance your DevOps toolkit! 💼🔧

Feel free to connect with me on LinkedIn 🤝 Check out my GitHub for more resources 📚

Happy learning, and stay tuned for more exciting DevOps content! 🚀🎉

About the author

  • 👩‍💻 As a seasoned software engineer with 7+ years of experience, my expertise includes:

    • 🌐 Developing web applications using Javascript (frontend and backend)
    • 💼 Working across diverse industries like construction, finance, marketing, and entrepreneurship
    • 🎓 Holding a postgraduate degree in Master of Computer Applications from the University of Pune

    🔧 My technical prowess lies in:

    • ⚛️ Leading Javascript frameworks: React, Node.js, and Express.js
    • 🚀 Delivering robust and scalable web applications with the best software engineering practices

    🌱 Excitingly, I've started my DevOps learning journey with #90DaysOfDevOps to enhance my skills in this area.

    🧠 Beyond technical skills, I offer:

    • 💡 Exceptional problem-solving abilities and a strong aptitude for learning new technologies
    • 🤝 A collaborative mindset, making me a valuable team player in diverse environments

    🚀 My passion is in delivering high-quality software solutions that solve real-world problems. If you're seeking a skilled and experienced software engineer for top-notch web applications, let's connect and explore potential opportunities together! 🤝

No comments yet. Be the first to comment.

More from this blog