mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
azure virtual machine facts -- querying all machines in subscription fix (#54075)
This commit is contained in:
parent
a03e69550c
commit
9c77509cbc
2 changed files with 39 additions and 18 deletions
|
@ -201,6 +201,7 @@ vms:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from msrestazure.azure_exceptions import CloudError
|
from msrestazure.azure_exceptions import CloudError
|
||||||
|
from msrestazure.tools import parse_resource_id
|
||||||
except Exception:
|
except Exception:
|
||||||
# This is handled in azure_rm_common
|
# This is handled in azure_rm_common
|
||||||
pass
|
pass
|
||||||
|
@ -248,8 +249,10 @@ class AzureRMVirtualMachineFacts(AzureRMModuleBase):
|
||||||
self.fail("Parameter error: resource group required when filtering by name.")
|
self.fail("Parameter error: resource group required when filtering by name.")
|
||||||
if self.name:
|
if self.name:
|
||||||
self.results['vms'] = self.get_item()
|
self.results['vms'] = self.get_item()
|
||||||
|
elif self.resource_group:
|
||||||
|
self.results['vms'] = self.list_items_by_resourcegroup()
|
||||||
else:
|
else:
|
||||||
self.results['vms'] = self.list_items()
|
self.results['vms'] = self.list_all_items()
|
||||||
|
|
||||||
return self.results
|
return self.results
|
||||||
|
|
||||||
|
@ -258,17 +261,14 @@ class AzureRMVirtualMachineFacts(AzureRMModuleBase):
|
||||||
item = None
|
item = None
|
||||||
result = []
|
result = []
|
||||||
|
|
||||||
try:
|
item = self.get_vm(self.resource_group, self.name)
|
||||||
item = self.compute_client.virtual_machines.get(self.resource_group, self.name)
|
|
||||||
except CloudError as err:
|
|
||||||
self.module.warn("Error getting virtual machine {0} - {1}".format(self.name, str(err)))
|
|
||||||
|
|
||||||
if item and self.has_tags(item.tags, self.tags):
|
if item and self.has_tags(item.get('tags'), self.tags):
|
||||||
result = [self.serialize_vm(item)]
|
result = [item]
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def list_items(self):
|
def list_items_by_resourcegroup(self):
|
||||||
self.log('List all items')
|
self.log('List all items')
|
||||||
try:
|
try:
|
||||||
items = self.compute_client.virtual_machines.list(self.resource_group)
|
items = self.compute_client.virtual_machines.list(self.resource_group)
|
||||||
|
@ -278,18 +278,31 @@ class AzureRMVirtualMachineFacts(AzureRMModuleBase):
|
||||||
results = []
|
results = []
|
||||||
for item in items:
|
for item in items:
|
||||||
if self.has_tags(item.tags, self.tags):
|
if self.has_tags(item.tags, self.tags):
|
||||||
results.append(self.serialize_vm(self.get_vm(item.name)))
|
results.append(self.get_vm(self.resource_group, item.name))
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def get_vm(self, name):
|
def list_all_items(self):
|
||||||
|
self.log('List all items')
|
||||||
|
try:
|
||||||
|
items = self.compute_client.virtual_machines.list_all()
|
||||||
|
except CloudError as exc:
|
||||||
|
self.fail("Failed to list all items - {0}".format(str(exc)))
|
||||||
|
|
||||||
|
results = []
|
||||||
|
for item in items:
|
||||||
|
if self.has_tags(item.tags, self.tags):
|
||||||
|
results.append(self.get_vm(parse_resource_id(item.id).get('resource_group'), item.name))
|
||||||
|
return results
|
||||||
|
|
||||||
|
def get_vm(self, resource_group, name):
|
||||||
'''
|
'''
|
||||||
Get the VM with expanded instanceView
|
Get the VM with expanded instanceView
|
||||||
|
|
||||||
:return: VirtualMachine object
|
:return: VirtualMachine object
|
||||||
'''
|
'''
|
||||||
try:
|
try:
|
||||||
vm = self.compute_client.virtual_machines.get(self.resource_group, name, expand='instanceview')
|
vm = self.compute_client.virtual_machines.get(resource_group, name, expand='instanceview')
|
||||||
return vm
|
return self.serialize_vm(vm)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
self.fail("Error getting virtual machine {0} - {1}".format(self.name, str(exc)))
|
self.fail("Error getting virtual machine {0} - {1}".format(self.name, str(exc)))
|
||||||
|
|
||||||
|
@ -302,7 +315,7 @@ class AzureRMVirtualMachineFacts(AzureRMModuleBase):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
result = self.serialize_obj(vm, AZURE_OBJECT_CLASS, enum_modules=AZURE_ENUM_MODULES)
|
result = self.serialize_obj(vm, AZURE_OBJECT_CLASS, enum_modules=AZURE_ENUM_MODULES)
|
||||||
resource_group = re.sub('\\/.*', '', re.sub('.*resourceGroups\\/', '', result['id']))
|
resource_group = parse_resource_id(result['id']).get('resource_group')
|
||||||
instance = None
|
instance = None
|
||||||
power_state = None
|
power_state = None
|
||||||
|
|
||||||
|
|
|
@ -332,7 +332,8 @@
|
||||||
publisher: Canonical
|
publisher: Canonical
|
||||||
sku: 16.04-LTS
|
sku: 16.04-LTS
|
||||||
version: latest
|
version: latest
|
||||||
|
tags:
|
||||||
|
abc: def
|
||||||
- assert:
|
- assert:
|
||||||
that:
|
that:
|
||||||
- azure_vm.properties.availabilitySet.id
|
- azure_vm.properties.availabilitySet.id
|
||||||
|
@ -344,10 +345,6 @@
|
||||||
name: "{{ vm_name2 }}"
|
name: "{{ vm_name2 }}"
|
||||||
register: results
|
register: results
|
||||||
|
|
||||||
- name: Just dump output
|
|
||||||
debug:
|
|
||||||
var: results
|
|
||||||
|
|
||||||
- name: Assert that facts module returned the second vm
|
- name: Assert that facts module returned the second vm
|
||||||
assert:
|
assert:
|
||||||
that:
|
that:
|
||||||
|
@ -358,6 +355,17 @@
|
||||||
- results.vms[0].resource_group == "{{ resource_group }}"
|
- results.vms[0].resource_group == "{{ resource_group }}"
|
||||||
- results.vms[0].power_state != None
|
- results.vms[0].power_state != None
|
||||||
|
|
||||||
|
- name: Retrieve facts by tags
|
||||||
|
azure_rm_virtualmachine_facts:
|
||||||
|
tags:
|
||||||
|
- abc:def
|
||||||
|
register: results
|
||||||
|
|
||||||
|
- name: Assert that facts module returned the second vm
|
||||||
|
assert:
|
||||||
|
that:
|
||||||
|
- results.vms | length >= 1
|
||||||
|
|
||||||
- name: Should be idempotent with a dual NICs
|
- name: Should be idempotent with a dual NICs
|
||||||
azure_rm_virtualmachine:
|
azure_rm_virtualmachine:
|
||||||
resource_group: "{{ resource_group }}"
|
resource_group: "{{ resource_group }}"
|
||||||
|
|
Loading…
Reference in a new issue