Day 15 of #90DaysOfDevops || Python Libraries for DevOps

Day 15 of #90DaysOfDevops || Python Libraries for DevOps

·

3 min read

As a DevOps engineer, one should be able to work with JSON and YAML files in Python. Python has many libraries that make it easy to parse and work with these formats.

1). JSON in Python

JSON is a data format that is easy to read and parse in the machine. It is widely used for API testing, web development, and as DevOps engineers.

Python has built-in libraries to parse JSON data. Two main methods used are Load() and Dump(). Basically, load() is used to parse JSON data to Python objects and dump() to parse Python objects to JSON data

2). YAML in Python

YAML is a human-readable data serialization format that is often used in configuration files. In DevOps, most of the manifestation will be with the YAML file. It is widely used for creating pods, deployments, and containerization.

Same as JSON libraries, The PyYAML library is a popular choice for working with YAML files in Python. The library provides two methods Load() and Dump(). The load() method converts a YAML string into a Python object and the dump() method converts a Python object into a YAML string.


Tasks

  1. Create a Dictionary in Python and write it to a json File.

     import json
    
     dict = {'name':'Harman','age':26,'city':'Panjab'}
    
     with open('dict.json', 'w') as f:
         json.dump(dict,f)
    

    In this code, we are creating a dictionary first and then we are creating a dict.json file to put JSON data into it. dump() method uses the dictionary created above and writes down to dict.json file.

    output:

     {
         "name": "Harman", 
         "age": 26, 
         "city": "Panjab"
     }
    
  2. Read a json file services.json kept in this folder and print the service names of every cloud service provider.

     {
         "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
           }
         }
       }
    

    To read this services.json file we will write a python parser which will convert this JSON data to python objects.

     import json
    
     json_file =  'c:/Users/hardy/Desktop/MAIN/extra/services.json'
    
     with open(json_file, 'r') as f:
         json_data = json.loads(f.read())
    
     for provider, services in json_data.items():
         print(f"{provider} : {services['service']} ")
    

    output:

    aws: ec2

    azure: VM

    gcp: compute engine

  3. Read YAML file using python, file services.yaml and read the contents to convert yaml to json

     ---
     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
    

    To convert yaml data to json format we will use python parser as:

    ```python

    import yaml import json

with open("c:/Users/hardy/Desktop/MAIN/extra/services.yaml", "r") as f: data = yaml.safe_load(f)

json_data = json.dumps(data)

print(json_data)


    This will print out data in json format as:

    ```json
    {
        "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
          }
        }
      }

End of the tasks.


Reach me on:

  1. Github-> https://github.com/Hrmn97

  2. Twitter -> https://twitter.com/Harman9765

  3. LinkedIn -> https://www.linkedin.com/in/chetan-harman-56310424a

  4. Website -> https://devhrmn.netlify.app/