DevOps, Day - 15

DevOps, Day - 15

Python Libraries for DevOps

Reading JSON and YAML in Python

Basic differences between YAML and JSON

  1. As a DevOps Engineer, you should be able to parse files, be it txt, json, yaml, etc.

    • As a DevOps Engineer, it's important to be able to read and work with different types of files, like text files, JSON files, YAML files, and so on. This is because we often need to extract useful information from these files to automate infrastructure and application deployment tasks.

    • In Python, there are various libraries that we can use to read and write different types of files. Once we've read a file, we can use different techniques to extract useful data from it. This might involve accessing specific fields in a dictionary, iterating over items in a list, or using regular expressions to find patterns in text data.

    • Being able to work with files is an important skill for any DevOps Engineer because it allows us to automate tasks and make our deployment workflows more efficient.

  2. Python has numerous libraries like os, sys, json, yaml etc that a DevOps Engineer uses in day-to-day tasks.

    • os - Provides functions for interacting with the operating system, including file and directory manipulation, process management, and more.

    • sys - Provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.

    • json - Provides functions for encoding Python objects as JSON and decoding JSON data into Python objects.

    • yaml - Provides tools for reading and writing YAML files, as well as converting YAML data to Python objects.

    • subprocess - Provides functions for managing subprocesses, running commands in a subprocess, and communicating with the subprocess through input/output pipes.

    • shutil - Provides functions for high-level file operations, such as copying, moving, and deleting files and directories.

    • paramiko - Provides an interface for securely connecting to remote hosts and running commands over SSH.

    • fabric - Provides a simple and consistent API for working with remote servers over SSH.

    • psutil - Provides functions for retrieving information about running processes and system utilization, including CPU, memory, disk usage, and more.

Tasks:

  1. Create a Dictionary in Python and write it to a json File.

    step 1: Import json library
    step 2: create a dictionary with key:values of your choice

    step 3: create a json file
    step 4: Now whatever dictionary you have created, just dump it to a file using dump()

    step 5: After executing this file, we get the output as...

  2. Read a json file file.json and print the contents of it

    step 1: create a file and Import json library
    step 2: open the created file in read format "r"
    step 3: By using iteration and load(), we can print the contents of our file by formatting it.

    step 4: After executing this, we get the output as...

  3. Read YAML file using the Python file file.yaml and read the contents to convert yaml to json
    step 1:
    create a file and import yaml and json libraries

    step 2: create a yaml file and add some contents of your choice in it.
    step 3: Now load it to the file and store that in a yaml_data variable.
    step 4: The line json_data = json.dumps(yaml_data) converts a Python object (dictionary) yaml_data to a JSON-formatted string and stores it in the variable json_data.
    step 5: Finally, print the contents of json_data.

    step 6: After executing this code, we get the output as...

Thank you so much for reading.

Follow me on LinkedIn to see interesting posts like this : )

Linkedin

Β