diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_scaleset_facts.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_scaleset_facts.py index 895c4260f6..283ae459e1 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_scaleset_facts.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine_scaleset_facts.py @@ -29,6 +29,17 @@ options: resource_group: description: - The resource group to search for the desired virtual machine scale set + format: + description: + - Format of the data returned. + - If C(raw) is selected information will be returned in raw format from Azure Python SDK. + - If C(curated) is selected the structure will be identical to input parameters of azure_rm_virtualmachine_scaleset module. + - In Ansible 2.5 and lower facts are always returned in raw format. + default: 'raw' + choices: + - 'curated' + - 'raw' + version_added: "2.6" extends_documentation_fragment: - azure @@ -42,6 +53,7 @@ EXAMPLES = ''' azure_rm_virtualmachine_scaleset_facts: resource_group: Testing name: testvmss001 + format: curated - name: Get facts for all virtual networks azure_rm_virtualmachine_scaleset_facts: @@ -60,81 +72,41 @@ azure_vmss: returned: always type: list example: [{ - "location": "eastus", - "properties": { - "overprovision": true, - "singlePlacementGroup": true, - "upgradePolicy": { - "mode": "Manual" - }, - "virtualMachineProfile": { - "networkProfile": { - "networkInterfaceConfigurations": [ - { - "name": "testvmss", - "properties": { - "dnsSettings": { - "dnsServers": [] - }, - "enableAcceleratedNetworking": false, - "ipConfigurations": [ - { - "name": "default", - "properties": { - "privateIPAddressVersion": "IPv4", - "subnet": { - "id": "/subscriptions/XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXX/resourceGroups/Testing/providers/Microsoft.Network/virtualNetworks/testvnet/subnets/testsubnet" - } - } - } - ], - "primary": true - } - } - ] - }, - "osProfile": { - "adminUsername": "testuser", - "computerNamePrefix": "testvmss", - "linuxConfiguration": { - "disablePasswordAuthentication": true, - "ssh": { - "publicKeys": [ - { - "keyData": "", - "path": "/home/testuser/.ssh/authorized_keys" - } - ] - } - }, - "secrets": [] - }, - "storageProfile": { - "imageReference": { - "offer": "CoreOS", - "publisher": "CoreOS", - "sku": "Stable", - "version": "899.17.0" - }, - "osDisk": { - "caching": "ReadWrite", - "createOption": "fromImage", - "managedDisk": { - "storageAccountType": "Standard_LRS" - } - } - } + "admin_username": "testuser", + "capacity": 2, + "data_disks": [ + { + "caching": "ReadWrite", + "disk_size_gb": 64, + "lun": 0, + "managed_disk_type": "Standard_LRS" } + ], + "image": { + "offer": "CoreOS", + "publisher": "CoreOS", + "sku": "Stable", + "version": "899.17.0" }, - "sku": { - "capacity": 1, - "name": "Standard_DS1_v2", - "tier": "Standard" - } + "load_balancer": null, + "location": "eastus", + "managed_disk_type": "Standard_LRS", + "name": "testVMSSeb4fd3c704", + "os_disk_caching": "ReadWrite", + "os_type": "Linux", + "resource_group": "myresourcegroup", + "ssh_password_enabled": false, + "state": "present", + "subnet_name": null, + "tier": "Standard", + "upgrade_policy": "Manual", + "virtual_network_name": null, + "vm_size": "Standard_DS1_v2" }] ''' # NOQA from ansible.module_utils.azure_rm_common import AzureRMModuleBase +import re try: from msrestazure.azure_exceptions import CloudError @@ -155,7 +127,13 @@ class AzureRMVirtualMachineScaleSetFacts(AzureRMModuleBase): self.module_args = dict( name=dict(type='str'), resource_group=dict(type='str'), - tags=dict(type='list') + tags=dict(type='list'), + format=dict( + type='str', + choices=['curated', + 'raw'], + default='raw' + ) ) self.results = dict( @@ -167,6 +145,7 @@ class AzureRMVirtualMachineScaleSetFacts(AzureRMModuleBase): self.name = None self.resource_group = None + self.format = None self.tags = None super(AzureRMVirtualMachineScaleSetFacts, self).__init__( @@ -187,6 +166,71 @@ class AzureRMVirtualMachineScaleSetFacts(AzureRMModuleBase): else: self.results['ansible_facts']['azure_vmss'] = self.list_items() + if self.format == 'curated': + for index in range(len(self.results['ansible_facts']['azure_vmss'])): + vmss = self.results['ansible_facts']['azure_vmss'][index] + subnet_name = None + load_balancer_name = None + virtual_network_name = None + ssh_password_enabled = False + + try: + subnet_id = (vmss['properties']['virtualMachineProfile']['networkProfile']['networkInterfaceConfigurations'][0] + ['properties']['ipConfigurations'][0]['properties']['subnet']['id']) + subnet_name = re.sub('.*subnets\\/', '', subnet_id) + except: + self.log('Could not extract subnet name') + + try: + backend_address_pool_id = (vmss['properties']['virtualMachineProfile']['networkProfile']['networkInterfaceConfigurations'][0] + ['properties']['ipConfigurations'][0]['properties']['loadBalancerBackendAddressPools'][0]['id']) + load_balancer_name = re.sub('\\/backendAddressPools.*', '', re.sub('.*loadBalancers\\/', '', backend_address_pool_id)) + virtual_network_name = re.sub('.*virtualNetworks\\/', '', re.sub('\\/subnets.*', '', subnet_id)) + except: + self.log('Could not extract load balancer / virtual network name') + + try: + ssh_password_enabled = (not vmss['properties']['virtualMachineProfile']['osProfile'], + ['linuxConfiguration']['disablePasswordAuthentication']) + except: + self.log('Could not extract SSH password enabled') + + data_disks = vmss['properties']['virtualMachineProfile']['storageProfile'].get('dataDisks', []) + + for disk_index in range(len(data_disks)): + old_disk = data_disks[disk_index] + new_disk = { + 'lun': old_disk['lun'], + 'disk_size_gb': old_disk['diskSizeGB'], + 'managed_disk_type': old_disk['managedDisk']['storageAccountType'], + 'caching': old_disk['caching'] + } + data_disks[disk_index] = new_disk + + updated = { + 'resource_group': self.resource_group, + 'name': vmss['name'], + 'state': 'present', + 'location': vmss['location'], + 'vm_size': vmss['sku']['name'], + 'capacity': vmss['sku']['capacity'], + 'tier': vmss['sku']['tier'], + 'upgrade_policy': vmss['properties']['upgradePolicy']['mode'], + 'admin_username': vmss['properties']['virtualMachineProfile']['osProfile']['adminUsername'], + 'admin_password': vmss['properties']['virtualMachineProfile']['osProfile'].get('adminPassword'), + 'ssh_password_enabled': ssh_password_enabled, + 'image': vmss['properties']['virtualMachineProfile']['storageProfile']['imageReference'], + 'os_disk_caching': vmss['properties']['virtualMachineProfile']['storageProfile']['osDisk']['caching'], + 'os_type': 'Linux' if (vmss['properties']['virtualMachineProfile']['osProfile'].get('linuxConfiguration') is not None) else 'Windows', + 'managed_disk_type': vmss['properties']['virtualMachineProfile']['storageProfile']['osDisk']['managedDisk']['storageAccountType'], + 'data_disks': data_disks, + 'virtual_network_name': virtual_network_name, + 'subnet_name': subnet_name, + 'load_balancer': load_balancer_name + } + + self.results['ansible_facts']['azure_vmss'][index] = updated + return self.results def get_item(self): diff --git a/test/integration/targets/azure_rm_virtualmachine_scaleset/tasks/main.yml b/test/integration/targets/azure_rm_virtualmachine_scaleset/tasks/main.yml index 99b637f1f2..0dff0d0a97 100644 --- a/test/integration/targets/azure_rm_virtualmachine_scaleset/tasks/main.yml +++ b/test/integration/targets/azure_rm_virtualmachine_scaleset/tasks/main.yml @@ -1,3 +1,8 @@ +- name: Prepare random number + set_fact: + rpfx: "{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}" + run_once: yes + - name: Create virtual network azure_rm_virtualnetwork: resource_group: "{{ resource_group }}" @@ -26,7 +31,7 @@ - name: Create VMSS azure_rm_virtualmachine_scaleset: resource_group: "{{ resource_group }}" - name: testVMSS + name: testVMSS{{ rpfx }} vm_size: Standard_DS1_v2 admin_username: testuser ssh_password_enabled: true @@ -50,14 +55,48 @@ managed_disk_type: Standard_LRS register: results -- name: Assert that VMSS ran +- name: Assert that VMSS was created assert: that: results.changed +- name: Retrieve scaleset facts + azure_rm_virtualmachine_scaleset_facts: + resource_group: "{{ resource_group }}" + name: testVMSS{{ rpfx }} + format: curated + register: output_scaleset + +- name: Get scaleset body + set_fact: + body: "{{ output_scaleset.ansible_facts.azure_vmss[0] }}" + +- name: Try to update VMSS using output as input + azure_rm_virtualmachine_scaleset: + resource_group: "{{ body.resource_group }}" + name: "{{ body.name }}" + vm_size: "{{ body.vm_size }}" + admin_username: "{{ body.admin_username }}" + ssh_password_enabled: "{{ body.ssh_password_enabled }}" + admin_password: "Password1234!" + capacity: "{{ body.capacity }}" + virtual_network_name: "{{ body.virtual_network_name }}" + subnet_name: "{{ body.subnet_name }}" + upgrade_policy: "{{ body.upgrade_policy }}" + tier: "{{ body.tier }}" + managed_disk_type: "{{ body.managed_disk_type }}" + os_disk_caching: "{{ body.os_disk_caching }}" + image: "{{ body.image }}" + data_disks: "{{ body.data_disks }}" + register: results + +- name: Assert that VMSS was updated + assert: + that: not results.changed + - name: Delete VMSS azure_rm_virtualmachine_scaleset: resource_group: "{{ resource_group }}" - name: testVMSS + name: testVMSS{{ rpfx }} state: absent remove_on_absent: ['all'] vm_size: Standard_DS1_v2 @@ -82,7 +121,7 @@ - name: Create VMSS (check mode) azure_rm_virtualmachine_scaleset: resource_group: "{{ resource_group }}" - name: testVMSS1 + name: testVMSS{{ rpfx }}1 vm_size: Standard_DS1_v2 admin_username: testuser ssh_password_enabled: true @@ -115,7 +154,7 @@ - name: Create VMSS (check mode) azure_rm_virtualmachine_scaleset: resource_group: "{{ resource_group }}" - name: testVMSS1 + name: testVMSS{{ rpfx }}1 vm_size: Standard_DS1_v2 admin_username: testuser ssh_password_enabled: true @@ -147,7 +186,7 @@ - name: Delete VMSS azure_rm_virtualmachine_scaleset: resource_group: "{{ resource_group }}" - name: testVMSS1 + name: testVMSS{{ rpfx }}1 state: absent remove_on_absent: ['all'] vm_size: Standard_DS1_v2