Execute Azure CLI Command

Overview

The Execute Azure CLI Command action allows users to execute Azure CLI commands directly from their bot workflow. This feature provides users with the flexibility to retrieve details about Azure resources efficiently. Users can input an Azure CLI command manually or leverage AI to generate commands based on specific needs.

Prerequisites

Linux Agent

  1. Agent Configuration:
    Ensure the Linux Agent is configured. Refer to the Agent Installation Guide.
  2. The Azure CLI must be installed and configured on the agent, with credentials that have the appropriate permissions for the intended Azure operations.
    1. Refer to the Azure CLI installation guide.

How to Use This Action?

To utilize the Execute Azure CLI Command action, follow these steps:

  • In your bot workflow, navigate to the Linux Actions and select Library.
    Navigate to Linux Action

  • Search for or locate the Execute Azure CLI Command action and drag it into your workflow.

  • Select the Linux integration that is connected to your Azure CLI-configured agent.

  • Under the Parameters section, specify the required parameter values according to your requirements. For more details, refer to the Parameter Details section.

  • In the az_command field, you can either:

    1. Manually enter the Azure CLI command you want to execute.
      Azure Command Parameter

    2. Use AI to generate the command by clicking on the "Generate with AI" button. For example, enter a prompt like "retrieve all resource groups."
      Azure Command AI

  • Save or update the bot, then click on the Run button to execute the bot or the Run button inside the action node. Wait for the execution to complete. After execution, view the results in the execution details.

Parameter Details

ParameterRequiredDescription
az_commandYesThe Azure CLI command to execute.
script_modeYesSpecifies whether the Azure CLI command is executed as a script (combination of multiple commands) or as individual commands. Set to True for script execution, or False for individual command execution. Defaults to False. NOTE: Check format limitations in Limitations.
use_az_cli_profileConditionalIndicates whether to use the Azure CLI profile directly (defaults to False). This is a straightforward method to get started. Ensure the Azure CLI is installed and configured. For advanced options, use azure_config_profile.
run_as_userYesSpecifies the user under which the command runs (defaults to "ubuntu."). Update the username if using a different cloud provider or user. Ensure Azure CLI is configured for this user.
azure_config_profileConditionalThe profile from which to load Azure credentials. If you want to use this instead of use_az_cli_profile, add the necessary credentials to the agent configuration file, including AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_TENANT_ID, and AZURE_SUBSCRIPTION_ID. For more information on the agent configuration file, refer to the Agent Installation Guide.
execution_timeoutYesThe maximum time (in seconds) allowed for command execution, defaulting to 300 seconds.

Using Handlebar

You can use the Handlebar capabilities to reference values dynamically from the output of a previous node. This enables the creation of flexible and reusable commands within your workflow.

Referencing Particular Values

For example, to stop a virtual machine in Azure, you can utilize the following Azure CLI command:

az vm stop --resource-group <resource-group-name> --name <vm-name> --no-wait

If the previous node returns the resource_group_name and vm_name keys, you can reference the values dynamically as follows:

az vm stop --resource-group {{$.nodes.list_vms[0].resource_group_name}} --name {{$.nodes.list_vms[0].vm_name}} --no-wait

Note: To reference values from previous nodes, type $$ to view and select the desired data dynamically.

Explanation

In this example:

  1. The resource_group_name and vm_name are retrieved dynamically from the list_vms node.
  2. The index 0 specifies that the first VM in the returned list will be referenced.

The generated command will dynamically substitute the values, resulting in:

az vm stop --resource-group MyResourceGroup --name MyWebServer --no-wait

Limitations

  1. Outputs: Each action executes commands with the default output format set to JSON.
    1. Script mode: Not recommended if you intend to use data from the current CLI node as input for the next node in the workflow.
    2. Non-script mode: The output may not always be in JSON format. This depends on Azure CLI behavior, as certain commands do not produce JSON outputs.

Example Use Case

Scenario: Identifying Untagged Virtual Machines

As a cloud administrator, you need to ensure compliance with your organization's tagging policy for Azure resources. During a recent audit, you found that some virtual machines (VMs) are missing important tags, which could lead to unforeseen costs.

To address this, you want to retrieve a list of all VMs in a specific Resource Group that do not have a tag named Environment. This will help you identify and rectify any discrepancies.

Retrieve VMs Without a Specific Tag: You will execute a command to filter and list all VMs in the MyResourceGroup that lack the Environment tag.

  1. Command:

    az vm list --resource-group MyResourceGroup --query "[?tags.Environment==null]"

    This command will return a list of VMs that do not have the `Environment` tag, allowing you to take action to update their tagging and ensure compliance.