Azure-Samples/virtual-machines-python-manage

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

alexlenail opened this issue · 3 comments

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

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)]

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

@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:
Azure/azure-sdk-for-python#713