Skip to content

Commit

Permalink
add missing hostvars properties in azure_rm.py inventory (ansible#53046)
Browse files Browse the repository at this point in the history
* add missing hostvars properties

* fix lint

* fix lint

* add security group

* fix lint
  • Loading branch information
yungezz authored and nitzmahone committed Mar 6, 2019
1 parent b090b57 commit 71042e1
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion lib/ansible/plugins/inventory/azure_rm.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
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):
Expand Down Expand Up @@ -499,7 +500,10 @@ def hostvars(self):
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
Expand All @@ -511,12 +515,48 @@ def hostvars(self):
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
Expand Down

0 comments on commit 71042e1

Please sign in to comment.