From 71042e1a79937177de324c93f5701ee642c8f7d8 Mon Sep 17 00:00:00 2001 From: Yunge Zhu <37337818+yungezz@users.noreply.github.com> Date: Thu, 7 Mar 2019 02:55:43 +0800 Subject: [PATCH] add missing hostvars properties in azure_rm.py inventory (#53046) * add missing hostvars properties * fix lint * fix lint * add security group * fix lint --- lib/ansible/plugins/inventory/azure_rm.py | 42 ++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/ansible/plugins/inventory/azure_rm.py b/lib/ansible/plugins/inventory/azure_rm.py index e7c70ec452b..a971012488c 100644 --- a/lib/ansible/plugins/inventory/azure_rm.py +++ b/lib/ansible/plugins/inventory/azure_rm.py @@ -162,6 +162,7 @@ from itertools import chain from msrest import ServiceClient, Serializer, Deserializer from msrestazure import AzureConfiguration from msrestazure.polling.arm_polling import ARMPolling +from msrestazure.tools import parse_resource_id class AzureRMRestConfiguration(AzureConfiguration): @@ -499,7 +500,10 @@ class AzureHost(object): vmss=dict( id=self._vmss['id'], name=self._vmss['name'], - ) if self._vmss else {} + ) if self._vmss else {}, + virtual_machine_size=self._vm_model['properties']['hardwareProfile']['vmSize'] if self._vm_model['properties'].get('hardwareProfile') else None, + plan=self._vm_model['properties']['plan']['name'] if self._vm_model['properties'].get('plan') else None, + resource_group=parse_resource_id(self._vm_model['id']).get('resource_group').lower() ) # set nic-related values from the primary NIC first @@ -511,12 +515,48 @@ class AzureHost(object): new_hostvars['private_ipv4_addresses'].append(private_ip) pip_id = ipc['properties'].get('publicIPAddress', {}).get('id') if pip_id: + new_hostvars['public_ip_id'] = pip_id + pip = nic.public_ips[pip_id] + new_hostvars['public_ip_name'] = pip._pip_model['name'] new_hostvars['public_ipv4_addresses'].append(pip._pip_model['properties'].get('ipAddress', None)) pip_fqdn = pip._pip_model['properties'].get('dnsSettings', {}).get('fqdn') if pip_fqdn: new_hostvars['public_dns_hostnames'].append(pip_fqdn) + new_hostvars['mac_address'] = nic._nic_model['properties'].get('macAddress') + new_hostvars['network_interface'] = nic._nic_model['name'] + new_hostvars['network_interface_id'] = nic._nic_model['id'] + new_hostvars['security_group_id'] = nic._nic_model['properties']['networkSecurityGroup']['id'] \ + if nic._nic_model['properties'].get('networkSecurityGroup') else None + new_hostvars['security_group'] = parse_resource_id(new_hostvars['security_group_id'])['resource_name'] \ + if nic._nic_model['properties'].get('networkSecurityGroup') else None + + # set image and os_disk + new_hostvars['image'] = {} + new_hostvars['os_disk'] = {} + storageProfile = self._vm_model['properties'].get('storageProfile') + if storageProfile: + imageReference = storageProfile.get('imageReference') + if imageReference: + if imageReference.get('publisher'): + new_hostvars['image'] = dict( + sku=imageReference.get('sku'), + publisher=imageReference.get('publisher'), + version=imageReference.get('version'), + offer=imageReference.get('offer') + ) + elif imageReference.get('id'): + new_hostvars['image'] = dict( + id=imageReference.get('id') + ) + + osDisk = storageProfile.get('osDisk') + new_hostvars['os_disk'] = dict( + name=osDisk.get('name'), + operating_system_type=osDisk.get('osType').lower() if osDisk.get('osType') else None + ) + self._hostvars = new_hostvars return self._hostvars