Day 15 Task: Python Libraries for DevOps
Hii I am a backend/DevOps engineer.I have a experience with development and automation.I mostly work with Python, django, Cloud based technologies.
🔹Python Libraries
As a DevOps Engineer, working with various file formats like JSON, YAML, and text files is a common task. Python provides several libraries to handle these file formats effectively. Let's explore some of the essential libraries used by DevOps Engineers:
os : The
oslibrary provides a way to interact with the operating system, such as accessing files and directories, and executing shell commands. It is commonly used in DevOps for tasks like file operations, directory manipulation, and environment handling.Example:
import os # Get the current working directory current_directory = os.getcwd() print("Current Directory:", current_directory) # List all files in the current directory files = os.listdir('.') print("Files in Current Directory:", files) # Create a new directory os.mkdir('new_directory') # Execute a shell command os.system('ls -l')sys: The
syslibrary provides access to system-specific parameters and functions. It allows you to interact with the Python interpreter, handle command-line arguments, and manipulate Python runtime environment.Example:
import sys # Get the command-line arguments arguments = sys.argv print("Command-line arguments:", arguments) # Get the Python version print("Python Version:", sys.version) # Terminate the Python program sys.exit(0)json:
jsonis a built-in Python library that provides methods to work with JSON (JavaScript Object Notation) data.It allows you to encode Python objects into JSON format (serialization) and decode JSON data into Python objects (deserialization).
DevOps Engineers often use JSON to represent configuration data and exchange data between different systems.
Example:
import json # Sample JSON data data = '{"name": "John", "age": 30, "city": "New York"}' # Decode JSON data to Python dictionary parsed_data = json.loads(data) print("Decoded JSON data:", parsed_data) # Encode Python dictionary to JSON data new_data = {"name": "Jane", "age": 25, "city": "London"} encoded_data = json.dumps(new_data) print("Encoded JSON data:", encoded_data)yaml:
yamlis not a standard library, but you can install thePyYAMLpackage to work with YAML (YAML Ain't Markup Language) data.YAML is a human-readable data serialization format, similar to JSON, and is commonly used for configuration files and data interchange.
With the
PyYAMLlibrary, you can read YAML data and convert it into Python objects and vice versa.Example:
import yaml # Sample YAML data data = ''' name: John age: 30 city: New York ''' # Parse YAML data to Python dictionary parsed_data = yaml.safe_load(data) print("Parsed YAML data:", parsed_data) # Convert Python dictionary to YAML data new_data = {'name': 'Jane', 'age': 25, 'city': 'London'} yaml_data = yaml.safe_dump(new_data) print("YAML data:", yaml_data)requestsLibrary: Therequestslibrary is a popular Python library that simplifies making HTTP requests and working with APIs. It provides an easy-to-use interface to interact with web services and perform various HTTP operations like GET, POST, PUT, DELETE, etc. This library handles low-level details of the HTTP protocol, allowing developers to focus on data retrieval and manipulation.requestsis widely used in DevOps for automating API calls, fetching data from web services, and integrating different systems.Example:
import requests # Send a GET request to a URL and fetch data response = requests.get("https://api.example.com/data") data = response.json() # Parse the response JSON data print(data)paramikoLibrary: Theparamikolibrary provides an implementation of SSH (Secure Shell) protocol in Python, enabling secure communication and remote execution on servers. It allows you to establish SSH connections, transfer files via SFTP (SSH File Transfer Protocol), and execute commands remotely on remote servers. DevOps engineers often useparamikoto manage server configurations, deploy applications, and automate server maintenance tasks securely.Example:
import paramiko # Create an SSH client ssh_client = paramiko.SSHClient() # Connect to the remote server ssh_client.connect('remote_server_ip', username='username', password='password') # Execute a command remotely stdin, stdout, stderr = ssh_client.exec_command('ls -l') # Print the output print(stdout.read().decode()) # Close the SSH connection ssh_client.close()boto3Library: Theboto3library is the official AWS SDK for Python, providing an easy-to-use interface for interacting with various AWS services. It enables DevOps engineers to automate tasks related to Amazon Web Services, such as creating and managing EC2 instances, working with S3 buckets, managing security groups, and more. Withboto3, you can programmatically access and manage AWS resources, making it a valuable tool for infrastructure automation and cloud management.Example:
import boto3 # Create an EC2 client ec2_client = boto3.client('ec2', region_name='us-west-2') # Describe EC2 instances response = ec2_client.describe_instances() # Print instance details for reservation in response['Reservations']: for instance in reservation['Instances']: print(f"Instance ID: {instance['InstanceId']}, State: {instance['State']['Name']}")
Tasks
Create a Dictionary in Python and write it to a json File.
import json # Create a sample dictionary data = { "aws": "ec2", "azure": "VM", "gcp": "compute engine" } # Write the dictionary to a JSON file with open("services.json", "w") as f: json.dump(data, f)Read a json file
services.jsonkept in this folder and print the service names of every cloud service provider.import json # Read the JSON file and print service names with open("services.json", "r") as f: services = json.load(f) for provider, service in services.items(): print(f"{provider} : {service}")output aws : ec2 azure : VM gcp : compute engineRead YAML file using python, file
services.yamland read the contents to convert yaml to json- Create the
services.yamlfile with the following content:
- Create the
aws: ec2
azure: VM
gcp: compute engine
- Use the
yamlandjsonlibraries in Python to read theservices.yamlfile, convert it to JSON, and print the JSON data. Make sure to install the required libraries by runningpip install pyyamlif you haven't already.
import yaml
import json
# Read the YAML file
with open("services.yaml", "r") as f:
yaml_data = yaml.safe_load(f)
# Convert YAML to JSON
json_data = json.dumps(yaml_data)
# Print the JSON data
print(json_data)
This script will read the data from the services.yaml file, which contains the specified content, and then convert it to JSON format. The output will be:
{"aws": "ec2", "azure": "VM", "gcp": "compute engine"}



