Python Libraries for DevOps

Photo by David Clode on Unsplash

Python Libraries for DevOps

Reading JSON and YAML in Python

As a DevOps Engineer, you should be able to parse files, whether it is JSON or YAML.

JSON: The purpose of JSON (JavaScript Object Notation) is to format data so that different computer programs can easily understand and exchange information. It is an important library in Python by default. Two important features are "loads" and "dumps".

  • Loads: Takes a JSON string and converts it into a Python object (dictionary).

      x = '{ "name":"Sam" }' #string
      y = json.loads(x) #command used to convert Json string into Python object (dictionary)
    
  • Dumps: Takes a Python dictionary and converts it to a JSON string.

  •           x = {                #python dictionary
                  "name":"Sam"
                  }
              y = json.dumps(x) #commands used to convert Python dictionary to JSON string
    

As a DevOps engineer, you can use JSON in various practical ways such as configuration management, API integration, Infrastructure Provisioning, etc. It provides a standardized & flexible format for data exchange between different systems/applications.

YAML: The purpose of a YAML (Yet Another Markup Language) file is to store and represent structured data in a human-readable and easily parseable format. YAML files are commonly used for configuration management, defining infrastructure as code, specifying application settings, and describing data structures in a variety of contexts. Some key concepts and features of YAML for DevOps Engineers are:

  1. Syntax: spacing/indentation is very important to keep in mind. As you can see in the below example there are spaces and indentations and one needs to make sure that it's the same amount of spacing in the structure.

     server : 
         name : "Sam's Server"
         capacity : 50GB
    
  2. Key-Value Pairs: Key on the left and values on the right and what separates them is the ":" colon. See below.

     name : "Sam's Server"
    
  3. Lists: You can show lists using hyphens for each item and you can have objects within a list.

     # List
     server_list :
         - server_1
         - server_2
         - server_3
     # List with Objects
     system_server_list :
         - server_1 : 
           name : "ec2" # Object in the list
           space : 50GB # Object in the list
    

Task-1: Create a Dictionary in Python and write it to a JSON File.

I have created a dictionary with employee titles & names and then it's written in JSON.

import json #importing JSON library

employee_list = {         #dictionary created in Python
    "CTO":"Splinter",
    "Director":"Leonardo",
    "Manager":"Donatello",
    "Supervisor":"Raphael",
    "admin":"Michelangelo"
}

json_object = json.dumps(employee_list)
print(json_object) #print/write as JSON

    # OUTPUT 
{"CTO":"Splinter", "Director":"Leonardo", "Manager":"Donatello", "Supervisor":"Raphael", "admin":"Michelangelo"}

Task 2: Read a JSON file services.json (file on GitHub Repo) and print the service names of every cloud service provider.

The file name "services.json" was used in Python to read the file.

{
    "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 json #import JSON library

with open('services.json') as json_file: #open() function opens the name of JSON file
    data = json.load(json_file) #load() function used to read the JSON file and assign it to "data" variable

print("aws",data['services']['aws']['name'])
print("azure:",data['services']['azure']['name'])
print("gcp:",data['services']['gcp']['name'])

The output of Task 2 is:

am@Sams-MacBook-Pro masterclass-python-for-devops % python3 services.py
aws EC2
azure: VM
gcp: Compute Engine

Task 3: Read YAML file using Python, file services.yaml (file on GitHub Repo) and read the contents to convert YAML to JSON.

The file name "services.yaml" was used to complete this task.

---
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 the above YAML file in Python and to convert it to JSON I used "PyMAL" library. If not installed, you can install it via the command "pip install pyyaml".

import yaml
import json

#Read YAML file 
with open('services.yaml', 'r') as yaml_file: 
    yaml_content = yaml_file.read()

#Convert YAML to JSON
json_content = json.dumps(yaml.safe_load(yaml_content), indent=4)

#Print the JSON data
print(json_content)

The output of Task 3 is:

sam@Sams-MacBook-Pro masterclass-python-for-devops % python3 yamltojson.py
{
    "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
        }
    }
}
sam@Sams-MacBook-Pro masterclass-python-for-devops %

I appreciate your busy time reading this short blog. As I continue with my journey to learn and acquire the skill set of a DevOps Engineer, I will share what I learn. Thank you.

Happy Learning!


Sam Samarullah

LinkedIn

Previous Blog