From 0b828ee8307f8145d8f7f3bfaa1c0459fa0bb4d3 Mon Sep 17 00:00:00 2001 From: Zim Kalinowski Date: Thu, 22 Mar 2018 07:30:42 +0800 Subject: [PATCH] azure_rm_virtualmachine: adding possibility of disabling public ip address (#36766) * adding possibility of disabling public ip address * fixed indent * fixed whitespace * fixed mistake * try to create test with vm without public ip address * try to fix test * another attempt for test * fixing test * create vm with no ip with different name and delete it immediately * a few additional fixes * another attempt to pass test * must be deleted * simplified no ip test * reorganised tests * Wrapped choice in C() --- .../cloud/azure/azure_rm_virtualmachine.py | 15 ++++++---- .../tasks/virtualmachine.yml | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py index bae26792f5..72c2f36a01 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachine.py @@ -202,9 +202,11 @@ options: - If a public IP address is created when creating the VM (because a Network Interface was not provided), determines if the public IP address remains permanently associated with the Network Interface. If set to 'Dynamic' the public IP address may change any time the VM is rebooted or power cycled. + - The C(Disabled) choice was added in Ansible 2.6. choices: - Dynamic - Static + - Disabled default: - Static aliases: @@ -646,7 +648,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase): default='ReadOnly'), managed_disk_type=dict(type='str', choices=['Standard_LRS', 'Premium_LRS']), os_type=dict(type='str', choices=['Linux', 'Windows'], default='Linux'), - public_ip_allocation_method=dict(type='str', choices=['Dynamic', 'Static'], default='Static', + public_ip_allocation_method=dict(type='str', choices=['Dynamic', 'Static', 'Disabled'], default='Static', aliases=['public_ip_allocation']), open_ports=dict(type='list'), network_interface_names=dict(type='list', aliases=['network_interfaces']), @@ -1641,8 +1643,11 @@ class AzureRMVirtualMachine(AzureRMModuleBase): if not subnet_id: self.fail(no_subnets_msg) - self.results['actions'].append('Created default public IP {0}'.format(self.name + '01')) - pip = self.create_default_pip(self.resource_group, self.location, self.name + '01', self.public_ip_allocation_method) + pip = None + if self.public_ip_allocation_method != 'Disabled': + self.results['actions'].append('Created default public IP {0}'.format(self.name + '01')) + pip_info = self.create_default_pip(self.resource_group, self.location, self.name + '01', self.public_ip_allocation_method) + pip = self.network_models.PublicIPAddress(id=pip_info.id, location=pip_info.location, resource_guid=pip_info.resource_guid) self.results['actions'].append('Created default security group {0}'.format(self.name + '01')) group = self.create_default_securitygroup(self.resource_group, self.location, self.name + '01', self.os_type, @@ -1661,9 +1666,7 @@ class AzureRMVirtualMachine(AzureRMModuleBase): parameters.network_security_group = self.network_models.NetworkSecurityGroup(id=group.id, location=group.location, resource_guid=group.resource_guid) - parameters.ip_configurations[0].public_ip_address = self.network_models.PublicIPAddress(id=pip.id, - location=pip.location, - resource_guid=pip.resource_guid) + parameters.ip_configurations[0].public_ip_address = pip self.log("Creating NIC {0}".format(network_interface_name)) self.log(self.serialize_obj(parameters, 'NetworkInterface'), pretty_print=True) diff --git a/test/integration/targets/azure_rm_virtualmachine/tasks/virtualmachine.yml b/test/integration/targets/azure_rm_virtualmachine/tasks/virtualmachine.yml index c97ac67f96..775909cc37 100644 --- a/test/integration/targets/azure_rm_virtualmachine/tasks/virtualmachine.yml +++ b/test/integration/targets/azure_rm_virtualmachine/tasks/virtualmachine.yml @@ -222,6 +222,35 @@ - assert: that: azure_publicipaddresses | length == 0 +- name: Create virtual machine without public ip address + register: output + azure_rm_virtualmachine: + resource_group: "{{ resource_group }}" + name: testvmnoip + vm_size: Standard_A0 + admin_username: adminuser + admin_password: Password123! + short_hostname: testvm + os_type: Linux + public_ip_allocation_method: Disabled + availability_set: "avbs{{ resource_group | hash('md5') | truncate(7, True, '') }}" + image: + offer: UbuntuServer + publisher: Canonical + sku: 16.04-LTS + version: latest + +- assert: + that: + - not 'publicIPAddress' in output.ansible_facts.azure_vm.properties.networkProfile.networkInterfaces[0].properties.ipConfigurations[0].properties + +- name: Delete VM with no public ip + azure_rm_virtualmachine: + resource_group: "{{ resource_group }}" + name: testvmnoip + state: absent + vm_size: Standard_A0 + # TODO: Until we have a module to create/delete images this is the best tests # I can do - name: assert error thrown with invalid image dict