From 1cc5196922caa0a4328052900452f7ed10aee484 Mon Sep 17 00:00:00 2001 From: Yunge Zhu <37337818+yungezz@users.noreply.github.com> Date: Tue, 26 Mar 2019 11:04:11 +0800 Subject: [PATCH] backport: add missing hostvars properties in azure_rm.py inventory (#53046) (#54318) * add missing hostvars properties in azure_rm.py inventory (#53046) * add missing hostvars properties * fix lint * fix lint * add security group * fix lint (cherry picked from commit 71042e1a79937177de324c93f5701ee642c8f7d8) * add changelog --- ...azure_rm_inventory_properties_missing.yaml | 2 + lib/ansible/plugins/inventory/azure_rm.py | 42 ++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/53046-azure_rm_inventory_properties_missing.yaml diff --git a/changelogs/fragments/53046-azure_rm_inventory_properties_missing.yaml b/changelogs/fragments/53046-azure_rm_inventory_properties_missing.yaml new file mode 100644 index 0000000000..70662fde98 --- /dev/null +++ b/changelogs/fragments/53046-azure_rm_inventory_properties_missing.yaml @@ -0,0 +1,2 @@ +bugfixes: +- azure_rm inventory plugin - fix missing hostvars properties (https://github.com/ansible/ansible/pull/53046) diff --git a/lib/ansible/plugins/inventory/azure_rm.py b/lib/ansible/plugins/inventory/azure_rm.py index 3607d0e9f0..e79d5c7bd9 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