Skip to main content

Command Palette

Search for a command to run...

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

Updated
β€’2 min read
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! πŸš€πŸŽ‰

More from this blog