Skip to main content

Command Palette

Search for a command to run...

Day 15 - Python Libraries for DevOps

Published
4 min readView as Markdown
Day 15 - Python Libraries for DevOps
T

Hey there! 👋

I'm Tushar Ranjan, a DevOps Engineer passionate. Currently, on a learning adventure, I'm here to share my journey and Blogs 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!

In the world of DevOps, automation and efficiency are key. Python, with its vast array of libraries, is a powerful tool for DevOps engineers. Today, we will dive into some essential Python libraries that are particularly useful for handling JSON and YAML files, two common formats in configuration management and data exchange.

What are Python Libraries?

A Python library is a collection of pre-written code that provides specific functionalities and can be imported and used in other Python programs. These libraries can contain functions, classes, and modules that save time and effort in development by providing pre-built solutions. They can be open-source or proprietary and are usually installed via Python’s package manager, pip.

In DevOps, Python libraries are invaluable for automating tasks, interacting with APIs and cloud services, and manipulating data. Common tasks include server configuration and management, software deployment, testing, and monitoring.

Reading JSON and YAML in Python

As a DevOps Engineer, you should be adept at parsing various file types, including txt, JSON, and YAML. Python provides several libraries that are crucial for these tasks, such as os, sys, json, and yaml.

Task 1: Create a Dictionary in Python and Write it to a JSON File

First, let's create a dictionary in Python and write it to a JSON file. Here’s an example:

import json
# Creating a dictionary
my_dict = {
    "name": "DevOps Engineer",
    "tools": ["Docker", "Kubernetes", "Ansible"],
    "experience": 5
}

# Writing the dictionary to a JSON file
with open('example.json', 'w') as json_file:
    json.dump(my_dict, json_file)

print("Dictionary written to example.json")

In this example:

  • We create a dictionary called my_dict with some key-value pairs.

  • We open a file named example.json in write mode.

  • Using json.dump(), we serialize the dictionary and write it to the file.

  • The with statement ensures that the file is properly closed after writing.

After running this code, you should see a file named example.json in your working directory containing the contents of the dictionary.

Task 2: Read a JSON File and Print Service Names

Next, let's read a JSON file named services.json and print the service names of each cloud service provider. Here’s how you can do it:

import json

# Reading the JSON file
with open('services.json', 'r') as json_file:
    data = json.load(json_file)

# Printing service names for each cloud provider
for provider, services in data.items():
    print(f"{provider} : {services['service_name']}")

Assuming services.json has the following content:

code{
    "aws": {"service_name": "ec2"},
    "azure": {"service_name": "VM"},
    "gcp": {"service_name": "compute engine"}
}

The output will be:

aws : ec2
azure : VM
gcp : compute engine

In this example:

  • We open and load the JSON file into a dictionary using json.load().

  • We iterate over the dictionary and print each provider and their respective service name.

Task 3: Read YAML File and Convert it to JSON

Finally, let’s read a YAML file named services.yaml and convert its contents to JSON. Here’s how you can achieve this using the yaml library:

First, install the PyYAML library if you haven’t already:

pip install pyyaml

Then, use the following code:

import yaml
import json

# Reading the YAML file
with open('services.yaml', 'r') as yaml_file:
    data = yaml.safe_load(yaml_file)

# Converting YAML data to JSON
json_data = json.dumps(data, indent=4)

print("YAML data converted to JSON:")
print(json_data)

Assuming services.yaml has the following content:

aws:
  service_name: ec2
azure:
  service_name: VM
gcp:
  service_name: compute engine

The output will be a JSON formatted string:

code{
    "aws": {
        "service_name": "ec2"
    },
    "azure": {
        "service_name": "VM"
    },
    "gcp": {
        "service_name": "compute engine"
    }
}

In this example:

  • We use yaml.safe_load() to read the YAML file into a Python dictionary.

  • We then convert this dictionary to a JSON formatted string using json.dumps().

Conclusion

Python libraries provide powerful tools for DevOps engineers to handle and manipulate various file formats. By mastering these libraries, you can automate many tasks, making your workflow more efficient and reliable. Today, we explored how to create and handle JSON and YAML files using Python, which are fundamental skills for any DevOps professional.

Thank you for reading! I hope you found this post helpful. Feel free to share your thoughts and experiences in the comments.

Happy coding!

~ Tushar Ranjan🙂

More from this blog

Tushar Ranjan's blog

72 posts