Skip to main content

Command Palette

Search for a command to run...

Python Libraries for DevOps

Published
2 min read
M

I am a DevOps Engineer at Fidelity National Information Services and responsible to automate the manual things and create CI/CD pipelines in software development lifecycle.

What is a JSON file?

  • JSON is a JavaScript Object Notation, It is a lightweight data-interchange format.

  • It is like plain text written in JavaScript Object Notation.

  • Python has numerous libraries like os, sys, json, yaml etc that a DevOps Engineer uses in day-to-day tasks.

What is a YAML file?

  • YAML stands for Yet Another Markup Language. It is used to write a configuration file for the applications.


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

import JSON 

#Create Dictionary 
data = {
    "name":"Madhup Pandey",
    "age":24,
    "Hobbies": ["DevOps","Cricket","Technology"]
}

#Writing to JSON file
with open(dump_data, "w") as jsonfile:
    json.dump(data, jsonfile)

#Printing message
print(f"Data has been converted into JSON file")

Here,

data, --> is a python dictionary

with open(), --> is a function to open a particular file and "w" is used to write in it.

json.dump(), --> function used to write to a json file.


Read a json file services.json kept in this folder and print the service names of every cloud service provider.

output

aws : ec2
azure : VM
gcp : compute engine
import json

#Read json file
with open("services.json", "r") as jsonfile:
    cloud = json.load(jsonfile)

#Print cloud providers
print(f"aws :", cloud['services']['aws']['name'])
print(f"azure :", cloud['services']['azure']['name'])
print(f"gcp :", cloud['services']['gcp']['name'])

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

import yaml
import json

with open("services.yml", "r") as yaml_file:
    yaml_data = yaml.safe_load(yaml_file)
    print(type(yaml_data))

with open("myfile.json", "w") as json_file:
    jsd = json.dump(yaml_data, json_file)
    print(jsd)

More from this blog

Madhup Pandey's blog

21 posts