Python is a really good scripting language when it comes to automation. The reason may be associated to its open-source nature and a vast active community.
After some research, I found the following python modules useful for DevOps:
sys
Provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter and modify elements of the runtime environment.
os
Provides a portable way of using operating system dependant functionalities.
boto3
It is an AWS SDK for python to create, configure and manage AWS services, such as Amazon EC2 and Amazon S3. The SDK provides an object-oriented API as well as low-level access to AWS services.
json
JSON(JavaScript Object Notation) is a lightweight data interchange format inspired by JavaScript object literal syntax(although it is not a strict subset of JavaScript).
It is language-independent and used to store/represent structured data.
In python, JSON is stored as a string.
The conversion of JSON into their respective Python objects is known as Deserialization.
Equivalence between python and JSON
JSON Object | Python Equivalent |
object | dict |
array | list, tuple |
string | str |
number | int, float |
true | True |
false | False |
null | None |
Knowledge of this module comes really handy if you are into web or automation.
yaml
Originally YAML stood for "Yet Another Markup Language.
Today it's full form is "YAML Ain't a Markup Language"; meaning that although structure of YAML resembles a markup language, it is not a document markup
If you have ever used or seen GitHub-Actions, you must know YAML. Look at the following example take from redhat.
#Comment: This is a supermarket list using YAML
#Note that - character represents the list
---
food:
- vegetables: tomatoes #first list item
- fruits: #second list item
citrics: oranges
tropical: bananas
nuts: peanuts
sweets: raisins
YAML is used for automation at various levels - Kubernetes, GitHub Actions, Ansible, etc. The module will be further understood as we use it.
Tasks
Task - 1
Create a Dictionary in Python and write it to a json File.
import json
# simple dictionary containing data
my_data = {
"name": "John Wick",
"age": 34,
"nationality": "Belarusian-American",
"is_hitman": False,
"grades": [90, 85, 92]
}
# file path for to write JSON data to
json_file_path = "data_dump.json"
# Write the data(dictionary) to a JSON file
with open(json_file_path, 'w') as json_file:
json.dump(my_data, json_file, indent=4)
print(f"Dictionary has been written to {json_file_path}")
Task-2
Read a json file services.json
kept in the home folder and print the service names of every cloud service provider.
import json
import pprint # to beautify print
with open("../services.json", "r") as f:
data = json.load(f)
print("Displaying the data : \n")
pprint.pprint(data)
Task-3
Read YAML file using python, file services.yaml
and read the contents to convert yaml to json.
import yaml
import pprint #to beautify print
with open("../services.yaml", "r") as f:
data = yaml.safe_load(f)
pprint.pprint(data)