Get Azure Automation Runbook Credential in Python

303 Views Asked by At

I'm trying to get an Automation Account credential in my Python 3.8 runbook using a managed identity.

The current code looks like this.

from azure.identity import ManagedIdentityCredential
from azure.mgmt.automation import AutomationClient 
from azure.mgmt.automation.operations import CredentialOperations 

def get_credential(credential):

    subscription_id = "xxxxxxxxxxxx"
    automation_account = "xxxxxxxxxxxx"

    # Create Managed Identity object
    auth_client = ManagedIdentityCredential()

    # Create Automation Client
    client = AutomationClient(auth_client, subscription_id)

    # Create Credential Operations Client
    credential_client = CredentialOperations(client._client, client._config, client._serialize, client._deserialize)

    # Get Credential
    cred = credential_client.get(subscription_id, automation_account, credential)

return cred

The code gets the credential, but the problem I noticed is that this code only gets the credential object, which doesn't include the actual password that I'm looking for.

https://learn.microsoft.com/en-us/python/api/azure-mgmt-automation/azure.mgmt.automation.operations.credentialoperations?view=azure-python#azure-mgmt-automation-operations-credentialoperations-get

I've tried to find another way of getting the actual password, but haven't yet managed to find one.

Anyone know?

1

There are 1 best solutions below

1
On BEST ANSWER

I have used the below code in Azure Automation Runbook to get the credentials.

import automationassets
from automationassets import AutomationAssetNotFound

# get a credential
cred = automationassets.get_automation_credential("{credential_name}")
print (cred["username"])
print (cred["password"])

Output-

enter image description here

You can also use this code in vs code by adding Azure Automation extension in it.

References-

Manage credentials in Azure Automation | Microsoft Learn.