๐Day 15 - Python Libraries for DevOps

Hey there! ๐
I'm Pratik R. Mandge, a DevOps Engineer passionate about all things AWS DevOps Technology. Currently on a learning adventure, I'm here to share my journey and Blog's in the world of cloud and DevOps.
๐ ๏ธ My focus? Making sense of AWS services, improving CI/CD, and diving into infrastructure as code. Whether you're fellow interns or curious enthusiasts, let's grow together in the vibrant DevOps space.
๐ Connect with me for friendly chats, shared experiences, and learning moments. Here's to embracing the learning curve and thriving in the exciting world of AWS DevOps Technology!
Follow me on LinkedIn: https://www.linkedin.com/in/pratik-mandge363
๐ JSON and YAML in Python
As a DevOps Engineer you should be able to parse files, be it txt, json, yaml, etc. You should know what all 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. DevOps Engineers frequently work with various file formats including JSON and YAML for configuration management, automation, and data exchange tasks.
๐ Advantages of JSON:
Simplicity: JSON has a simple and straightforward syntax, making it easy for both humans to read and write, and for machines to parse and generate.
Widespread Support: JSON is widely supported across different programming languages and platforms, making it an excellent choice for interoperability and data exchange between systems.
Data Types: JSON supports a limited set of data types, including strings, numbers, Booleans, arrays, and objects, which makes it suitable for many common use cases.
Standardization: JSON has a standardized format and parsing rules, ensuring consistency in how data is represented and processed across different systems.
๐ Advantages of YAML:
Human-Readable: YAML is designed to be easily readable and writable by humans, with a syntax that closely resembles natural language. This makes it particularly well-suited for configuration files and other scenarios where human readability is important.
Expressiveness: YAML provides a more expressive syntax than JSON, allowing for more complex data structures and inline comments, which can improve readability and maintainability of configuration files.
Data Serialization: YAML supports a wider range of data types compared to JSON, including null values, dates, and complex data structures like lists of dictionaries. This flexibility makes it suitable for a broader range of use cases.
Multi-Language Support: While YAML is primarily used in the context of Python and other scripting languages, it is also commonly used in other programming environments, such as configuration files for Kubernetes and Ansible playbooks.
๐
Python has numerous libraries like os, sys, json, yaml etc that a DevOps Engineer uses in day to day tasks.
os: The
osmodule provides a portable way of using operating system-dependent functionality, such as file operations, directory manipulation, and environment variables.sys: The
sysmodule provides access to system-specific parameters and functions, including interaction with the Python interpreter and command-line arguments.json: The
jsonmodule allows for parsing JSON data and encoding Python objects into JSON format, which is commonly used for data exchange and configuration files.yaml (PyYAML): The
yamlmodule (often used in conjunction with PyYAML library) enables parsing and serialization of YAML data, which is commonly used for configuration files and data serialization.
๐ Task 1: Create a Dictionary and Write to a JSON File.
#First step is make .json file named as a "service.json"
{
"aws": "ec2",
"azure": "VM",
"gcp": "compute engine"
}
๐
Task 2: Read a .json file services.json kept in this folder and print the service names of every cloud service provider.
#Second Step is make "services_json.py" file
import json
with open('services.json', 'r') as f:
services = json.load(f)
print("Service Names for Each Cloud Provider:")
for provider, service in services.items():
print(f"{provider}: {service}")
# For Showing Output open terminal & write "python services_json.py"

๐ Task 3: Read a YAML File and Convert it to JSON
To read a YAML file using Python and convert it to JSON, you can use the pyyaml library, which is commonly used for working with YAML data. First, you need to install the library using pip:
pip install pyyaml
Now, let's read a YAML file and convert it to JSON:
import yaml
import json
with open('services.yaml', 'r') as yaml_file:
yaml_data = yaml.safe_load(yaml_file)
# Convert YAML data to JSON
json_data = json.dumps(yaml_data, indent=2)
print(json_data)
In this code, we read a YAML file using yaml.safe_load and then use json.dumps to convert the YAML data to JSON format.
I'm confident that this article will prove to be valuable, helping you discover new insights and learn something enriching.๐
Thank you : )




