Day15 of #90daysofdevops

Important packages in python for Devops engineers.

Today we are going to learn how to work with JSON and yaml files within python.

As a DevOps Engineer, you should be able to parse files, be it txt, json, yaml, etc. well, what exactly parsing in python?

  • A parser in Python refers to a module or a set of modules that are used to convert a human-readable source code written in a programming language into a more machine-readable form. It takes the source code as input, analyzes it, and transforms it into an abstract syntax tree (AST), which is a tree-like structure representing the different elements of the source code such as functions, classes, variables, and expressions.

The abstract syntax tree is then used by the compiler or interpreter to generate machine code, which can be executed by the computer.

In Python, the built-in module ast provides a set of functions and classes to parse and manipulate the abstract syntax tree of a Python program. Another popular parser library for Python is pyparsing.

  • You should know what libraries one should use in Python for DevOps. Python has numerous libraries like os, sys, json, yaml etc that a DevOps Engineer uses in day-to-day tasks. Tasks Create a Dictionary in Python and write it to a json File.
services.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
      }
    }
  }
services.yaml
---
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

Tasks

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

    • Here is an example of creating a dictionary in python and writing it to a json file.
```json
import json

# Create a dictionary
fav_tools = {
    1: "Linux",
    2: "Git",
    3: "Docker",
    4: "Kubernetes",
    5: "Terraform",
    6: "Ansible",
    7: "Chef"
}

# Write the dictionary to a json file
with open("fav_tools.json", "w") as file:
    json.devopstools(fav_tools, file)
```

The json.devopstools the function is used to write the dictionary to a JSON file. The with statement is used to open the file, write to it and automatically close it when the block of code is executed.

  1. Read a json file services.json kept above and print the service names of every cloud service provider.
output

aws : ec2
azure : VM
gcp : compute engine

Here is an example in Python to read the services.json file and print the service names:

import json

with open("services.json") as f:
    data = json.load(f)

services = data["services"]
for cloud_service, details in services.items():
    if cloud_service != "debug":
        print(f"{cloud_service}: {details['name']}")

output :

aws: EC2
azure: VM
gcp: Compute Engine
  1. Read YAML file using python, file services.yaml and read the contents to convert yaml to json
  • You can use the PyYaml library to read the YAML file and convert it to a JSON file using the following code:

      import yaml
      import json
    
      with open("services.yaml") as file:
          yaml_data = yaml.safe_load(file)
    
      json_data = json.dumps(yaml_data)
    
      with open("services.json", "w") as file:
          file.write(json_data)
    

    Here, In this code, yaml.safe_load the method is used to parse the contents of the YAML file and convert it to a Python dictionary. The json.dumps the method is then used to convert the dictionary to a JSON string. The JSON string is then written to a file named services.json using the write method of the file object.

Hope you got some grip on working with JSON, YAML files with python

That's it for Today. Thanks for your time.

Please share your valuable feedback by liking 👍, sharing🤝 and commenting.

See you tomorrow, with another blog.

#day15 #90DaysOfDevops #python #DevOps #json #yaml

Did you find this article valuable?

Support Nagacharan by becoming a sponsor. Any amount is appreciated!