From dfd998bcbc98c11154cb8a7576408b4ac3732e12 Mon Sep 17 00:00:00 2001 From: Sean Whitesell Date: Tue, 17 Dec 2019 22:55:33 -0600 Subject: [PATCH] Added ability to specify plan information for images that require it. (#65335) * Modified to include plan information. * Fixed where plan is passed to the virtual machine object. * Added changelog file * Update changelogs/fragments/65335-add-plan-to-azure-vmscaleset-module.yaml Co-Authored-By: Felix Fontein * Added plan suboptions with required flag. Removed code block checking plan since suboptions were added. * Changed true to True. Added space after commas where failed tests indicated. * Removed extra blank line. Added promotion_code to plan param list. * Trying to fix indention issue * Trying to fix indention * Changed example capacity to trigger build check. Last failure was not due to code. * Removed property for accepting terms and code block using it.. * Removed extra unneeded spaces. --- ...5-add-plan-to-azure-vmscaleset-module.yaml | 2 + .../azure/azure_rm_virtualmachinescaleset.py | 79 ++++++++++++++++++- 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/65335-add-plan-to-azure-vmscaleset-module.yaml diff --git a/changelogs/fragments/65335-add-plan-to-azure-vmscaleset-module.yaml b/changelogs/fragments/65335-add-plan-to-azure-vmscaleset-module.yaml new file mode 100644 index 00000000000..1b48dff1a35 --- /dev/null +++ b/changelogs/fragments/65335-add-plan-to-azure-vmscaleset-module.yaml @@ -0,0 +1,2 @@ +minor_changes: + - azure_rm_virtualmachinescaleset - added ability to use plan information for images that require billing plan details diff --git a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset.py b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset.py index da27187dfc8..7dfcf806160 100644 --- a/lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset.py +++ b/lib/ansible/modules/cloud/azure/azure_rm_virtualmachinescaleset.py @@ -207,6 +207,27 @@ options: type: bool default: True version_added: "2.9" + plan: + description: + - Third-party billing plan for the VM. + version_added: "2.10" + type: dict + suboptions: + name: + description: + - Billing plan name. + required: true + product: + description: + - Product name. + required: true + publisher: + description: + - Publisher offering the plan. + required: true + promotion_code: + description: + - Optional promotion code. zones: description: - A list of Availability Zones for your virtual machine scale set. @@ -257,6 +278,36 @@ EXAMPLES = ''' caching: ReadWrite managed_disk_type: Standard_LRS +- name: Create VMSS with an image that requires plan information + azure_rm_virtualmachinescaleset: + resource_group: myResourceGroup + name: testvmss + vm_size: Standard_DS1_v2 + capacity: 3 + virtual_network_name: testvnet + upgrade_policy: Manual + subnet_name: testsubnet + admin_username: adminUser + ssh_password_enabled: false + ssh_public_keys: + - path: /home/adminUser/.ssh/authorized_keys + key_data: < insert yor ssh public key here... > + managed_disk_type: Standard_LRS + image: + offer: cis-ubuntu-linux-1804-l1 + publisher: center-for-internet-security-inc + sku: Stable + version: latest + plan: + name: cis-ubuntu-linux-1804-l1 + product: cis-ubuntu-linux-1804-l1 + publisher: center-for-internet-security-inc + data_disks: + - lun: 0 + disk_size_gb: 64 + caching: ReadWrite + managed_disk_type: Standard_LRS + - name: Create a VMSS with a custom image azure_rm_virtualmachinescaleset: resource_group: myResourceGroup @@ -453,7 +504,10 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): overprovision=dict(type='bool', default=True), single_placement_group=dict(type='bool', default=True), zones=dict(type='list'), - custom_data=dict(type='str') + custom_data=dict(type='str'), + plan=dict(type='dict', options=dict(publisher=dict(type='str', required=True), + product=dict(type='str', required=True), name=dict(type='str', required=True), + promotion_code=dict(type='str'))), ) self.resource_group = None @@ -487,6 +541,7 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): self.single_placement_group = None self.zones = None self.custom_data = None + self.plan = None required_if = [ ('state', 'present', [ @@ -758,6 +813,12 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): if nsg: self.security_group = self.network_models.NetworkSecurityGroup(id=nsg.get('id')) + plan = None + if self.plan: + plan = self.compute_models.Plan(name=self.plan.get('name'), product=self.plan.get('product'), + publisher=self.plan.get('publisher'), + promotion_code=self.plan.get('promotion_code')) + os_profile = None if self.admin_username or self.custom_data or self.ssh_public_keys: os_profile = self.compute_models.VirtualMachineScaleSetOSProfile( @@ -779,6 +840,7 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): capacity=self.capacity, tier=self.tier, ), + plan=plan, virtual_machine_profile=self.compute_models.VirtualMachineScaleSetVMProfile( os_profile=os_profile, storage_profile=self.compute_models.VirtualMachineScaleSetStorageProfile( @@ -852,6 +914,21 @@ class AzureRMVirtualMachineScaleSet(AzureRMModuleBase): vmss_resource.virtual_machine_profile.storage_profile.data_disks = data_disks + if self.plan: + try: + plan_name = self.plan.get('name') + plan_product = self.plan.get('product') + plan_publisher = self.plan.get('publisher') + term = self.marketplace_client.marketplace_agreements.get( + publisher_id=plan_publisher, offer_id=plan_product, plan_id=plan_name) + term.accepted = True + self.marketplace_client.marketplace_agreements.create( + publisher_id=plan_publisher, offer_id=plan_product, plan_id=plan_name, parameters=term) + except Exception as exc: + self.fail(("Error accepting terms for virtual machine {0} with plan {1}. " + + "Only service admin/account admin users can purchase images " + + "from the marketplace. - {2}").format(self.name, self.plan, str(exc))) + self.log("Create virtual machine with parameters:") self.create_or_update_vmss(vmss_resource)