Python Libraries for DevOps

Python Libraries for DevOps

#90 Days of DevOps Challenge - Day 15

DevOps is a rapidly growing field, and Python is one of the most popular languages for automation and tooling. As a DevOps engineer, having a good understanding of key Python libraries can greatly enhance your efficiency and effectiveness. In this post, we’ll take a look at 10 must-know Python libraries for DevOps engineers, and provide use cases and simple examples for each one.

  • Ansible:- Ansible is an open-source automation tool that can be used for configuration management, application deployment, and task automation. It allows you to write simple, human-readable playbooks that can be used to automate complex IT tasks. Use case: Automating the deployment of a web application on multiple servers.

  • Boto3:- Boto3 is the Amazon Web Services (AWS) SDK for Python, which allows you to interact with AWS services such as S3, EC2, and RDS using Python. This library makes it easy to write Python scripts to manage your AWS infrastructure. Use case: Automating the creation of Amazon EC2 instances.

  • Fabric:- Fabric is a Python library and command-line tool for executing shell commands remotely over SSH. It simplifies the process of executing commands on multiple servers and can be used for tasks such as deployment, management, and monitoring. Use case: Automating the deployment of a web application on multiple servers.

  • JenkinsAPI:- JenkinsAPI is a Python wrapper for the Jenkins REST API that allows you to programmatically interact from Jenkins server. You can use it to create, update, and manage Jenkins jobs, as well as to retrieve build information and statistics. Use case: Automating the creation of Jenkins jobs.

  • PyYAML:- PyYAML is a Python library for parsing and emitting YAML. It can be used to parse YAML files containing configuration information, and it’s commonly used in conjunction with Ansible. Use case: Parsing a YAML file containing Ansible playbook.

JSON in Python:-

JSON (JavaScript Object Notation) is a lightweight and easily readable data format that’s widely used for data exchange between different systems and platforms, making it an essential component of modern DevOps workflows.

Think of it like a digital language that can describe different types of information, like text, numbers, lists, and objects.

JSON is widely used in web applications and APIs to transfer data between servers and clients, and it’s an essential tool for modern software development.

Example:-

import json

# A sample JSON string
json_string = '{"name": "Saikat", "age": 25, "email": "saikat55mukherjee@gmail.com"}'

# Decode the JSON string into a Python object
python_object = json.loads(json_string)

# Access and print a value from the decoded Python object
print(f"Name: {python_object['name']}")

# Access and print another value from the decoded Python object
print(f"Age: {python_object['age']}")

YAML in Python:-

YAML is a human-readable data-serialization language and stands for “YAML Ain’t Markup Language”, often also referred to as “Yet Another Markup Language”. It is written with a .yml or .yaml (preferred) file extension.

It is used because of its readability to write configuration settings for applications. It is user-friendly and easy to understand.

Example:-

import yaml
data = {
    'Name':'Saikat Mukherjee',
    'Position':'DevOps Engineer',
    'Location':'Kolkata',
    'Age':'25',
    'Experience': {'GitHub':'Software Engineer',\
    'Google':'Technical Engineer', 'Linkedin':'Data Analyst'},
    'Languages': {'Markup':['HTML'], 'Programming'\
    :['Python', 'JavaScript','Golang']}
}
yaml_output = yaml.dump(data, sort_keys=False) 

print(yaml_output)

Tasks:-

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

import json

# Data to be written
dictionary = {
    "name": "Saikat Mukherjee",
    "location": "Kolkata",
    "age": 25,
    "phonenumber": "9976770500"
}

# Serializing json
json_object = json.dumps(dictionary, indent=4)

# Writing to sample.json
with open("sample.json", "w") as outfile:
    outfile.write(json_object)

Output:-

  • 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

{
     "services": {
       "debug": "on",
       "aws": {
         "name": "EC2",
         "type": "pay per hour",
         "instances": 500,
         "count": 500
       },
       "azure": {
         "name": "VM",
         "type": "pay per hour",
         "instances": 500,
         "count": 500
       },
       "gcp": {
         "name": "Compute Engine",
         "type": "pay per hour",
         "instances": 500,
         "count": 500
       }
     }
   }
import json
 output= open('services.json')
   data = json.load(output)

 print("aws: ", data['services']['aws']['name'])
 print("azure: ", data['services']['azure']['name'])
 print("gcp: ", data['services']['gcp']['name'])

Output:-

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

services:
  debug: 'on'
  aws:
    name: EC2
    type: pay per hour
    instances: 500
    count: 500
  azure:
    name: VM
    type: pay per hour
    instances: 500
    count: 500
  gcp:
    name: Compute Engine
    type: pay per hour
    instances: 500
    count: 500
import yaml
import json

with open('services.yaml','r') as x:
  data = yaml.safe_load(x)

with open ('NewFile.json','w') as json_file:
  json.dump(data,json_file)


Output = json.dumps(json.load(open('NewFile.json')),indent=2)
print("json_file: \n", Output)

Before running above command we need to run below command

pip install pyyaml

Output:-

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any queries or corrections to be done to this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Did you find this article valuable?

Support Saikat Mukherjee's blog by becoming a sponsor. Any amount is appreciated!