Skip to content
This repository has been archived by the owner on Jun 17, 2024. It is now read-only.

An example illustrating how to use Python to manage your Azure Virtual Machines

License

Notifications You must be signed in to change notification settings

Azure-Samples/virtual-machines-python-manage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

page_type description languages products
sample
How to perform common management tasks with Microsoft Azure Virtual Machines using the Azure SDK for Python.
python
azure
azure-virtual-machines

Azure Virtual Machines Management Samples - Python

These samples demonstrate how to perform common management tasks with Microsoft Azure Virtual Machines using the Azure SDK for Python. The code provided shows how to do the following:

  • Create virtual machines:
    • Create a Linux virtual machine
    • Create a Windows virtual machine
  • Update a virtual machine:
    • Expand a drive
    • Tag a virtual machine
    • Attach data disks
    • Detach data disks
  • Operate a virtual machine:
    • Start a virtual machine
    • Stop a virtual machine
    • Restart a virtual machine
  • List virtual machines
  • Delete a virtual machine

To see the code to perform these operations, check out the run_example() function in example.py. Each operation is clearly labeled with a comment and a print function. The examples are not necessarily in the order shown in the above list.

Running this sample

  1. If you don't already have it, install Python.

    This sample (and the SDK) is compatible with Python 2.7, 3.4, 3.5, 3.6 and 3.7.

  2. General recommendation for Python development is to use a Virtual Environment. For more information, see https://docs.python.org/3/tutorial/venv.html

    Install and initialize the virtual environment with the "venv" module on Python 3 (you must install virtualenv for Python 2.7):

    python -m venv mytestenv # Might be "python3" or "py -3.6" depending on your Python installation
    cd mytestenv
    source bin/activate      # Linux shells (Bash, ZSH, etc.)
    scripts\activate         # Windows shells (PowerShell, CMD)
    
  3. Clone the repository.

    git clone https://github.com/Azure-Samples/virtual-machines-python-manage.git
    
  4. Install the dependencies using pip.

    cd virtual-machines-python-manage
    pip install -r requirements.txt
    
  5. Create an Azure service principal either through Azure CLI, PowerShell or the portal.

    Retrieve the application ID (a.k.a. client ID), authentication key (a.k.a. client secret), tenant ID and subscription ID from the Azure portal for use in the next step. This document describes where to find them (besides the subscription ID, which is in the "Overview" section of the "Subscriptions" blade.)

  6. Fill in and export these environment variables into your current shell.

    export AZURE_TENANT_ID={your tenant id}
    export AZURE_CLIENT_ID={your client id}
    export AZURE_CLIENT_SECRET={your client secret}
    export AZURE_SUBSCRIPTION_ID={your subscription id}
    
  7. Run the sample.

    python example.py
    

Notes

Retrieving a VM's OS disk

You may be tempted to try to retrieve a VM's OS disk by using virtual_machine.storage_profile.os_disk. In some cases, this may do what you want, but be aware that it gives you an OSDisk object. In order to update the OS Disk's size, as example.py does, you need not an OSDisk object but a Disk object. example.py gets the Disk object with the following:

os_disk_name = virtual_machine.storage_profile.os_disk.name
os_disk = compute_client.disks.get(GROUP_NAME, os_disk_name)

More information

Here are some helpful links:

If you don't have a Microsoft Azure subscription you can get a FREE trial account here.


Azure Stack Sample

The Azure Stack sample under Hybrid folder has moved to https://github.com/Azure-Samples/Hybrid-Compute-Python-Manage-VM


This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.

About

An example illustrating how to use Python to manage your Azure Virtual Machines

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages