Azure-Samples / virtual-machines-python-manage

An example illustrating how to use Python to manage your Azure Virtual Machines
MIT License
104 stars 84 forks source link

Is there some way to get a set of virtual machines which share a tag? #2

Closed alexlenail closed 8 years ago

lmazuel commented 8 years ago

There is no filtering option inside the list operations. I suggest you to do it using a list comprehensions:

def tag_is_present(tags_dict):
    return tags_dict and tags_dict.get('mytag') == 'myvalue'
tagged_vms = [vm for vm in compute_client.virtual_machines.list_all() if tag_is_present(vm.tags)]
alexlenail commented 8 years ago

I am personally of the belief that there should be a filtering method provided by the API, but I agree this represents a viable way of solving this problem. Thanks @lmazuel

lmazuel commented 8 years ago

@zfrenchee One other possibility is to use the generic list resources, filtering by tag: https://msdn.microsoft.com/en-us/library/azure/dn790569.aspx

You will not get a VirtualMachine object and you might miss some properties: you might want to do a get on the compute client to get them. But at least it will be filtered. Depending of your volume and situation could help.

The generic list with filter readthedocs link.

Useful reference: https://github.com/Azure/azure-sdk-for-python/issues/713