# Day 15: Python Libraries for DevOps

## Python Libraries for DevOps:

* Many Python libraries can be used in DevOps tasks.
    
    1. Ansible - A popular automation tool that allows managing and configuring servers, networks, and other infrastructure.
        
    2. Terraform - Infrastructure as a Code tool to manage and provision infrastructure across multiple cloud providers.
        
    3. Docker SDK for Python - A library for interacting with Docker containers and images.
        
    4. Kubernetes Python Client - A library for interacting with Kubernetes clusters.
        
    5. Fabric - A library for automating SSH tasks like running commands on remote servers, uploading files, and managing deployments.
        
    6. Paramiko - A library for working with SSH connections. It provides a low-level API for working with SSH sessions.
        
    7. GitPython - A library for working with Git repositories. It provides a high-level API for tasks like cloning repositories, managing branches, and committing changes.
        
    8. Pytest - A testing framework for Python. It can be used to automate the testing of applications, scripts, and infrastructure code.
        
    9. Boto3 - A Python library for working with Amazon Web Services (AWS) resources.
        
    10. Netmiko - A library for managing and automating network devices.
        
    
    ### **Reading JSON and YAML in Python**
    
    * As a DevOps Engineer, you should be able to parse files, be it Txt, json, yaml, etc.
        
    * You should know what libraries one should use in Python for DevOps.
        
    * Python has numerous libraries like `os`, `sys`, `JSON`, `yaml` etc that a DevOps Engineer uses in day-to-day tasks.
        
    
    ## Task1:-
    
* Create a Dictionary in Python and write it to a JSON File.
    
    **json. dumps()**: It is used to convert a dictionary to a JSON string.
    

* ```plaintext
    import json
     person = {
         "name": "Pooja",
         "age": 25,
         "city": "Pune"
     }                                # Create a dictionary
     with open('person.json', 'w') as json_file:
         json.dump(person, json_file)   # Write the dictionary to a JSON file
    ```
    

* we create a dictionary called a `person` with three key-value pairs: `name`, `age`, and `city`.
    
    To write this dictionary into a JSON file, we first use the `open()` function to create a new file called `person.json` in write mode (`'w'`). Then, we use the `JSON.dump()` function to write the contents of the `person's` dictionary to the file.
    
    After running this code, you should see a new file called `person.json` in your current directory, containing the following JSON data:
    
* ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682944698958/c35a3a39-b084-4153-8c62-96462e29246f.png align="center")
    

## Task2:-

Read a JSON file `services.json` kept in this folder and print the service names of every cloud service provider.

1. Follow the steps:- In the **services.json** file add the below content.
    

**services.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  
}  
}  
}

1. Create another Python file eg. ok.py and paste the below content in it.
    
    ```plaintext
    import json
    
    # Open the JSON file and load its contents
    with open('services.json', 'r') as file:
        data = json.load(file)
        print("aws:",data['services']['aws']['name'])
        print("azure:",data['services']['azure']['name'])
        print("gcp:",data['services']['gcp']['name'])
    ```
    

**json.load():** json.load() accepts the file object, parses the JSON data, populates a Python dictionary with the data, and returns it to you.

**Output:-**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682945987730/6008156f-18c8-4465-bc9b-b5db1f99a205.png align="center")

## Task3:-

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

```plaintext
import yaml
 from yaml.loader import SafeLoader
 import json

 # Open the YAML file and load its contents
 with open('services.yaml', 'r') as file:
     yaml_data = yaml.load(file, Loader=yaml.FullLoader)
 print(yaml_data)
```

**yaml. load()** function parses and converts a YAML object to a Python dictionary.

**Output:-**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1682593582222/555a7e92-137b-4c05-89d5-dd34f9b6a95f.png?auto=compress,format&format=webp&auto=compress,format&format=webp align="left")
