Day - 15

(Of 90 Days DevOps)

·

2 min read

Day - 15

Python Libraries for DevOps

Reading JSON and YAML in Python:

  • DevOps Engineers need to parse various file formats, including txt, json, and yaml.

  • Python offers essential libraries for DevOps tasks, such as os, sys, json, and yaml.

  • Proficiency in these libraries allows efficient handling and manipulation of different file types.

  • Navigating and extracting data from diverse file formats enhances a DevOps Engineer's effectiveness.

Tasks:

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

  1. 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
    

    Answer:

    1. Read YAML file using python, file services.yaml and read the contents to convert yaml to json

Answer: The detailed steps to complete task3 are as follows.

1. Install prerequisites

  • Ensure Python 3 is installed

  • Install PyYAML module

pip install pyyaml

2. Create sample YAML file

  • In your code folder, create a file called services.yaml

  • Add YAML content like:

3. Create python file & add below parts of code

  • Imports yaml and json modules

  • Loads data.yaml into yaml_data

  • Converts yaml_data to JSON string json_data

  • Writes json_data to new file data.json

  • Prints confirmation message

Output - The above code will print:

The services.json files contains:

Thanks for reaading my blog!

I'd love if you could share this article with anyone else who may find it useful!

Have a good day!