agent: |
Delete Unattached Public IP Addresses in Azure
This runbook identifies and removes public IP addresses that are not associated with any Azure resources. These unattached IPs can accrue unnecessary costs and pose potential security risks. By periodically cleaning up these unused IP addresses, organizations can optimize their Azure expenses and maintain a tidier, more secure environment.
- 1nQgDM95JkpjjQ71IRHWpGet Azure Subscription Id from CLI
1
Get Azure Subscription Id from CLI
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.This task retrieves the unique identifier for an Azure subscription using the Azure CLI. This ID is essential for managing resources and services tied to a specific Azure subscription programmatically.
inputsoutputsimport json try: result = _exe(None, "az account show") account_info = json.loads(result) subscription_id = account_info["id"] print("Fetched Subscription Id") print(subscription_id) # for debugging except json.JSONDecodeError: print("Error decoding JSON response from Azure CLI.") subscription_id = Nonecopied1 - 2CW0JA7eBzXsSkAwm9EXFList All Public IP Addresses in Azure
2
List All Public IP Addresses in Azure
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.This task retrieves and displays all public IP addresses provisioned within an Azure subscription, regardless of their association with resources.
inputsoutputsimport azure.identity from azure.mgmt.network import NetworkManagementClient # Initialize Azure credentials credential = azure.identity.DefaultAzureCredential() # Optional location parameter. Set to None for all regions, or specify a region like "eastus". #location = 'eastus' # or "eastus", "westus", etc. # Setup Azure Network client network_client = NetworkManagementClient(credential, subscription_id) try: # List all public IP addresses all_ips = list(network_client.public_ip_addresses.list_all()) # Filter by location if specified if location: all_ips = [ip for ip in all_ips if ip.location == location] if all_ips: print(f"Public IP Addresses in {'all regions' if not location else location}:") print("-" * 60) print(f"{'Name':<20} | {'IP Address':<20} | {'Location':<10}") print("-" * 60) for ip in all_ips: print(f"{ip.name:<20} | {ip.ip_address:<20} | {ip.location:<10}") print("-" * 60) else: print(f"No Public IP addresses found in {'your Azure subscription' if not location else location}.") except azure.core.exceptions.HttpResponseError as hr_err: print(f"Azure SDK Error: {hr_err.message}") except Exception as e: print(f"An unexpected error occurred: {e}")copied2 - 3bu67esnMas7evt04YOilFilter Out Unattached Public IP Addresses in Azure
3
Filter Out Unattached Public IP Addresses in Azure
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.This task sifts through all the public IP addresses within an Azure subscription based on specific criteria and condition. This action aids in segregating IPs, such as identifying unattached or unused addresses, facilitating efficient resource management, and optimizing costs in the Azure cloud environment.
inputsoutputsimport azure.identity from azure.mgmt.network import NetworkManagementClient # Initialize Azure credentials credential = azure.identity.DefaultAzureCredential() # Optional location parameter. Set to None for all regions, or specify a region like "eastus". #location = 'eastus' # or "eastus", "westus", etc. # Setup Azure Network client network_client = NetworkManagementClient(credential, subscription_id) try: # Filter by location if specified if location: all_ips = [ip for ip in all_ips if ip.location == location] unattached_ips = [ip for ip in all_ips if not ip.ip_configuration] # Display unattached public IPs in a table format if unattached_ips: print("\nUnattached Public IP Addresses:") print("-" * 60) print(f"{'Name':<20} | {'IP Address':<20} | {'Location':<10}") print("-" * 60) for ip in unattached_ips: print(f"{ip.name:<20} | {ip.ip_address:<20} | {ip.location:<10}") print("-" * 60) else: if location: print(f"No unattached public IP addresses found in {location}.") else: print("No unattached public IP addresses found in your Azure subscription.") except azure.core.exceptions.HttpResponseError as hr_err: print(f"Azure SDK Error: {hr_err.message}") except Exception as e: print(f"An unexpected error occurred: {e}")copied3 - 4Hjd6E7PLGhXWWzgkRkUDDelete Public IP Addresses in Azure
4
Delete Public IP Addresses in Azure
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.This task removes specific public IP addresses from an Azure subscription. This helps in decluttering the environment, ensuring efficient utilization of resources, and avoiding unnecessary costs associated with unused IP addresses within the Azure platform.
inputsoutputsimport azure.identity from azure.mgmt.network import NetworkManagementClient # Initialize Azure credentials credential = azure.identity.DefaultAzureCredential() # Setup Azure Network client network_client = NetworkManagementClient(credential, subscription_id) try: # Check if there are any unattached IPs if unattached_ips: # Display header for unattached public IPs being deleted print("\nInitiating deletion for the following unattached public IP addresses:") print("-" * 60) print(f"{'Name':<20} | {'IP Address':<20} | {'Location':<10}") print("-" * 60) # Initiating deletion for each unattached IP and displaying it for ip in unattached_ips: # Extract resource group from IP's ID try: resource_group = ip.id.split("/")[4] except IndexError: resource_group = None if resource_group: network_client.public_ip_addresses.begin_delete(resource_group, ip.name) print(f"{ip.name:<20} | {ip.ip_address:<20} | {ip.location:<10}") else: print(f"Failed to extract resource group for IP: {ip.name}") print("-" * 60) else: print("No unattached Public IP addresses were passed to this task to delete.") except Exception as e: print(f"An error occurred: {e}")copied4