Skip to content

Commit 90b5db5

Browse files
authored
Merge pull request #201038 from SnehaSudhirG/09June-managedidentites
Added content in use runbook authentication with managed identities
2 parents bcd337e + c129fdf commit 90b5db5

File tree

3 files changed

+75
-25
lines changed

3 files changed

+75
-25
lines changed

articles/automation/automation-hrw-run-runbooks.md

Lines changed: 75 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,42 +78,92 @@ You can also use an [InlineScript](automation-powershell-workflow.md#use-inlines
7878

7979
Hybrid Runbook Workers on Azure virtual machines can use managed identities to authenticate to Azure resources. Using managed identities for Azure resources instead of Run As accounts provides benefits because you don't need to:
8080

81-
* Export the Run As certificate and then import it into the Hybrid Runbook Worker.
82-
* Renew the certificate used by the Run As account.
83-
* Handle the Run As connection object in your runbook code.
81+
- Export the Run As certificate and then import it into the Hybrid Runbook Worker.
82+
- Renew the certificate used by the Run As account.
83+
- Handle the Run As connection object in your runbook code.
8484

85-
Follow the next steps to use a managed identity for Azure resources on a Hybrid Runbook Worker:
85+
There are two ways to use the Managed Identities in Hybrid Runbook Worker scripts.
8686

87-
1. Create an Azure VM.
88-
1. Configure managed identities for Azure resources on the VM. See [Configure managed identities for Azure resources on a VM using the Azure portal](../active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm.md#enable-system-assigned-managed-identity-on-an-existing-vm).
89-
1. Give the VM access to a resource group in Resource Manager. Refer to [Use a Windows VM system-assigned managed identity to access Resource Manager](../active-directory/managed-identities-azure-resources/tutorial-windows-vm-access-arm.md#grant-your-vm-access-to-a-resource-group-in-resource-manager).
90-
1. Install the Hybrid Runbook Worker on the VM. See [Deploy a Windows Hybrid Runbook Worker](automation-windows-hrw-install.md) or [Deploy a Linux Hybrid Runbook Worker](automation-linux-hrw-install.md).
91-
1. Update the runbook to use the [Connect-AzAccount](/powershell/module/az.accounts/connect-azaccount) cmdlet with the `Identity` parameter to authenticate to Azure resources. This configuration reduces the need to use a Run As account and perform the associated account management.
87+
1. Use the system-assigned Managed Identity for the Automation account:
88+
89+
1. [Configure](/enable-managed-identity-for-automation.md#enable-a-system-assigned-managed-identity-for-an-azure-automation-account) a System-assigned Managed Identity for the Automation account.
90+
1. Grant this identity the [required permissions](/enable-managed-identity-for-automation.md#assign-role-to-a-system-assigned-managed-identity) within the Subscription to perform its task.
91+
1. Update the runbook to use the [Connect-AzAccount](/powershell/module/az.accounts/connect-azaccount) cmdlet with the `Identity` parameter to authenticate to Azure resources. This configuration reduces the need to use a Run As account and perform the associated account management.
92+
93+
```powershell
94+
# Ensures you do not inherit an AzContext in your runbook
95+
Disable-AzContextAutosave -Scope Process
96+
97+
# Connect to Azure with system-assigned managed identity
98+
$AzureContext = (Connect-AzAccount -Identity).context
99+
100+
# set and store context
101+
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile
102+
$AzureContext
103+
104+
# Get all VM names from the subscription
105+
Get-AzVM -DefaultProfile $AzureContext | Select Name
106+
```
107+
> [!NOTE]
108+
> It is **Not** possible to use the Automation Account's User Managed Identity on a Hybrid Runbook Worker, it must be the Automation Account's System Managed Identity.
109+
110+
2. Use the VM Managed Identity for both the Azure VM or Arc-enabled server running as a Hybrid Runbook Worker.
111+
Here, you can use either the **VM’s User-assigned Managed Identity** or the **VM’s System-assigned Managed Identity**.
112+
113+
> [!NOTE]
114+
> This will **Not** work in an Automation Account which has been configured with an Automation account Managed Identity. As soon as the Automation account Managed Identity is enabled, you can't use the VM Managed Identity. The only available option is to use the Automation Account **System-Assigned Managed Identity** as mentioned in option 1.
115+
116+
**To use a VM's system-assigned managed identity**:
117+
118+
1. [Configure](/active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm#enable-system-assigned-managed-identity-on-an-existing-vm) a System Managed Identity for the VM.
119+
1. Grant this identity the [required permissions](/active-directory/managed-identities-azure-resources/tutorial-windows-vm-access-arm#grant-your-vm-access-to-a-resource-group-in-resource-manager) within the subscription to perform its tasks.
120+
1. Update the runbook to use the [Connect-Az-Account](/powershell/module/az.accounts/connect-azaccount?view=azps-8.0.0) cmdlet with the `Identity` parameter to authenticate to Azure resources. This configuration reduces the need to use a Run As Account and perform the associated account management.
92121
93122
```powershell
94-
# Ensures you do not inherit an AzContext in your runbook
95-
Disable-AzContextAutosave -Scope Process
96-
97-
# Connect to Azure with system-assigned managed identity
98-
$AzureContext = (Connect-AzAccount -Identity).context
99-
100-
# set and store context
101-
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
123+
# Ensures you do not inherit an AzContext in your runbook
124+
Disable-AzContextAutosave -Scope Process
125+
126+
# Connect to Azure with system-assigned managed identity
127+
$AzureContext = (Connect-AzAccount -Identity).context
128+
129+
# set and store context
130+
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile
131+
$AzureContext
132+
133+
# Get all VM names from the subscription
134+
Get-AzVM -DefaultProfile $AzureContext | Select Name
135+
```
136+
137+
**To use a VM's user-assigned managed identity**:
138+
1. [Configure](/active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vm#user-assigned-managed-identity) a User Managed Identity for the VM.
139+
1. Grant this identity the [required permissions](/active-directory/managed-identities-azure-resources/howto-assign-access-portal) within the Subscription to perform its tasks.
140+
1. Update the runbook to use the [Connect-AzAccount](/powershell/module/az.accounts/connect-azaccount?view=azps-8.0.0) cmdlet with the `Identity ` and `AccountID` parameters to authenticate to Azure resources. This configuration reduces the need to use a Run As account and perform the associated account management.
102141
103-
# Get all VM names from the subscription
104-
Get-AzVM -DefaultProfile $AzureContext | Select Name
142+
```powershell
143+
# Ensures you do not inherit an AzContext in your runbook
144+
Disable-AzContextAutosave -Scope Process
145+
146+
# Connect to Azure with user-managed-assigned managed identity. Replace <ClientId> below with the Client Id of the User Managed Identity
147+
$AzureContext = (Connect-AzAccount -Identity -AccountId <ClientId>).context
148+
149+
# set and store context
150+
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile
151+
$AzureContext
152+
153+
# Get all VM names from the subscription
154+
Get-AzVM -DefaultProfile $AzureContext | Select Name
105155
```
156+
> [!NOTE]
157+
> You can find the client Id of the user-assigned managed identity in the Azure portal.
158+
159+
> :::image type="content" source="./media/automation-hrw-run-runbooks/managed-identities-client-id-inline.png" alt-text="Screenshot of client id in Managed Identites." lightbox="./media/automation-hrw-run-runbooks/managed-identities-client-id-expanded.png":::
106160
107-
If you want the runbook to execute with the system-assigned managed identity, leave the code as-is. If you run the runbook in an Azure sandbox instead of Hybrid Runbook Worker and you want to use a user-assigned managed identity, then:
108-
1. From line 5, remove `$AzureContext = (Connect-AzAccount -Identity).context`,
109-
1. Replace it with `$AzureContext = (Connect-AzAccount -Identity -AccountId <ClientId>).context`, and
110-
1. Enter the Client ID.
111161
112162
>[!NOTE]
113-
>By default, the Azure contexts are saved for use between PowerShell sessions. It is possible that when a previous runbook on the Hybrid Runbook Worker has been authenticated with Azure, that context persists to the disk in the System PowerShell profile, as per [Azure contexts and sign-in credentials | Microsoft Docs](/powershell/azure/context-persistence?view=azps-7.3.2).
163+
> By default, the Azure contexts are saved for use between PowerShell sessions. It is possible that when a previous runbook on the Hybrid Runbook Worker has been authenticated with Azure, that context persists to the disk in the System PowerShell profile, as per [Azure contexts and sign-in credentials | Microsoft Docs](/powershell/azure/context-persistence?view=azps-7.3.2).
114164
For instance, a runbook with `Get-AzVM` can return all the VMs in the subscription with no call to `Connect-AzAccount`, and the user would be able to access Azure resources without having to authenticate within that runbook. You can disable context autosave in Azure PowerShell, as detailed [here](/powershell/azure/context-persistence?view=azps-7.3.2#save-azure-contexts-across-powershell-sessions).
115165
116-
166+
117167
### Use runbook authentication with Hybrid Worker Credentials
118168
119169
Instead of having your runbook provide its own authentication to local resources, you can specify Hybrid Worker Credentials for a Hybrid Runbook Worker group. To specify a Hybrid Worker Credentials, you must define a [credential asset](./shared-resources/credentials.md) that has access to local resources. These resources include certificate stores and all runbooks run under these credentials on a Hybrid Runbook Worker in the group.
67.1 KB
Loading
67.1 KB
Loading

0 commit comments

Comments
 (0)