Skip to main content

Command Palette

Search for a command to run...

Day 15 :- Python Libraries for DevOps

Published
3 min read
Day 15  :- Python Libraries for DevOps

Content :

Understanding the diferent Libraries of python used in the DevOps engineer in the day to day tasks.

Task :

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

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

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

Understanding the Libraries in python :-

Indeed, as a DevOps Engineer, being proficient in parsing various types of files is crucial for tasks like configuration management, automation, logging, monitoring, and more. Python provides a rich set of libraries for handling different types of files and tasks commonly encountered in DevOps workflows. Here are some essential libraries and modules frequently used by DevOps Engineers:

  1. os: Provides a way of using operating system-dependent functionality. Commonly used for file system operations, environment variables, process management, etc.

  2. sys: Provides access to some variables used or maintained by the Python interpreter and to functions that interact strongly with the interpreter. Useful for system-specific parameters and functions.

  3. json: Used for encoding and decoding JSON data. Often employed for managing configuration files, APIs, and interchanging data between systems.

  4. yaml: Handles YAML (YAML Ain't Markup Language) files, used for configuration files, data exchange, and readability.

  5. requests: Simplifies sending HTTP requests and handling responses, commonly used for interacting with APIs, web services, and performing HTTP operations.

  6. docker / docker-py: Used for Docker container management, allowing you to control Docker containers and interact with the Docker daemon through Python code.

  7. kubernetes: Python client library for Kubernetes. It allows you to interact with Kubernetes clusters to manage applications, deploy containers, and automate Kubernetes tasks.

  8. ansible: Although primarily a configuration management tool, Ansible provides a Python API that can be utilized for various automation tasks.

Tasks :-

import json
import yaml

# Step 1: Create a dictionary in Python
cloud_services = {
    "aws": "ec2",
    "azure": "VM",
    "gcp": "compute engine"
}

# Step 2: Write the dictionary to a JSON file
with open('cloud_services.json', 'w') as json_file:
    json.dump(cloud_services, json_file, indent=4)

# Step 3: Read the services.json file and print the service names of every cloud service provider
with open('services.json', 'r') as json_file:
    services_data = json.load(json_file)

for provider, service in services_data.items():
    print(f"{provider}: {service}")

# Step 4: Read the services.yaml file and convert YAML to JSON
with open('services.yaml', 'r') as yaml_file:
    services_yaml = yaml.safe_load(yaml_file)

# Convert YAML to JSON
services_json = json.dumps(services_yaml, indent=4)

# Print the JSON equivalent of the YAML file
print("JSON equivalent of services.yaml:")
print(services_json)

Make sure you have services.json and services.yaml files in the same folder as your Python script. This script will create a new JSON file named cloud_services.json containing the dictionary cloud_services, print service names from services.json, and convert services.yaml to JSON format and print its contents.

More from this blog

blog by yogesh ✍

23 posts