# Managing Compute Instances with Azure Run Commands and Ansible
This document provides a guide to writing Ansible playbooks that use version 3.15.0 of the Azure collection to manage and interact with Azure virtual machines. Specifically, this guide focuses on using the ability to execute commands on virtual machines through the Azure Virtual Machine Agent. Using this method does not require the management SSH key pairs on your Azure virtual machines, thus simplifying your configuration.
Note
By default, Run Command uses an elevated user. No additional steps are necessary to run privileged commands. Access may be limited if desired.
# Understanding the Agent Command Modules
The azure.azcollection.azure_rm_virtualmachineextension module allows you to execute scripts on Azure virtual machines. This module leverages the Azure VM Agent, which is a lightweight service that runs on Azure virtual machines and enables you to execute commands or manage other features on the VM without SSH access.
# Basic Playbook Example
Here is a simple example of an Ansible playbook that runs a command on an OCI compute instance.
# Execute command on Azure vm
---
hosts: localhost
tasks:
- name: Execute script and get output
azure.azcollection.azure_rm_virtualmachineextension:
resource_group: myResourceGroup
name: run-script-extension
virtual_machine_name: myVM
publisher: Microsoft.Azure.Extensions
virtual_machine_extension_type: CustomScript
type_handler_version: '2.1'
settings:
commandToExecute: "bash -c 'echo \"Hello World\" > /tmp/output.txt && cat /tmp/output.txt'"
register: script_result
- name: Display script output
debug:
var: script_result.properties.instanceView.substatuses[0].message
# Restrictions
The following restrictions apply when you’re using Run Command:
- Output is limited to the last 4,096 bytes.
- The minimum time to run a script is about 20 seconds.
- Scripts run by default as an elevated user on Linux.
- You can run one script at a time.
- Scripts that prompt for information (interactive mode) aren’t supported.
- You can’t cancel a running script.
- The maximum time a script can run is 90 minutes. After that, the script will time out.
- Outbound connectivity from the VM is required to return the results of the script.
# Additional Resources
· Azure Run Commands Documentation: https://learn.microsoft.com/en-us/azure/virtual-machines/run-command-overview
· Ansible Documentation: https://docs.ansible.com/
· Ansible Azure Collection: https://docs.ansible.com/projects/ansible/latest/collections/azure/index.html
# Conclusion
The azure.azcollection.azure_rm_virtualmachineextension module in the Azure Ansible collection provides a powerful and reliable way to manage Azure virtual machines through Ansible automation. By leveraging the VM Agent, you can execute commands and manage your infrastructure without the complexity of SSH key management or direct network access.
As you develop your Ansible playbooks, remember to follow best practices for security, error handling, and idempotency. Start with simple commands and gradually build more complex automation workflows as you become familiar with the module’s capabilities and limitations.