Skip to content

Commit

Permalink
Support for creating VMS through community images (#1759)
Browse files Browse the repository at this point in the history
* Support to create VM from community gallery image ID

* Add test case
  • Loading branch information
Fred-sun authored Nov 27, 2024
1 parent 3a0b12d commit 78ada32
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
24 changes: 24 additions & 0 deletions plugins/modules/azure_rm_virtualmachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@
description:
- The image used to build the VM.
- For custom images, the name of the image. To narrow the search to a specific resource group, a dict with the keys I(name) and I(resource_group).
- For community images, a dict with the keys I(community_gallery_image_id). Specified the community gallery image unique id for vm demployment.
- The I(community_gallery_image_id) can be fetched from shared gallery image GET call.
- For Marketplace images, a dict with the keys I(publisher), I(offer), I(sku), and I(version).
- Set I(version=latest) to get the most recent version of a given image.
- Required when creating.
Expand Down Expand Up @@ -840,6 +842,22 @@
sku: 20_04-lts
version: latest
- name: Create VM with cummunity gallery image ID
azure_rm_virtualmachine:
resource_group: "{{ resource_group }}"
name: "communit-image-vm"
admin_username: testuser
ssh_password_enabled: false
managed_disk_type: Premium_LRS
open_ports:
- 33
ssh_public_keys:
- path: /home/testuser/.ssh/authorized_keys
key_data: "ssh-rsa ************* @qq.com"
vm_size: Standard_B1ms
image:
community_gallery_image_id: "/CommunityGalleries/yellowbrick-fc7e81f1-87dd-4989-9ca8-03743762e873/Images/Ubuntu-5.15.0-1035-azure_22.04"
- name: Power Off
azure_rm_virtualmachine:
resource_group: myResourceGroup
Expand Down Expand Up @@ -1440,6 +1458,8 @@ def exec_module(self, **kwargs):
sku=self.image['sku'],
version=self.image['version']
)
elif self.image.get('community_gallery_image_id') is not None:
image_reference = self.compute_models.ImageReference(community_gallery_image_id=self.image['community_gallery_image_id'])
elif self.image.get('name'):
custom_image = True
image_reference = self.get_custom_image_reference(
Expand Down Expand Up @@ -2194,6 +2214,10 @@ def exec_module(self, **kwargs):
image_reference = self.compute_models.ImageReference(
id=vm_dict['storage_profile']['image_reference']['id']
)
elif 'community_gallery_image_id' in vm_dict['storage_profile'].keys():
image_reference = self.compute_models.ImageReference(
community_gallery_image_id=vm_dict['storage_profile']['image_reference']['community_gallery_image_id']
)
else:
image_reference = self.compute_models.ImageReference(
publisher=vm_dict['storage_profile']['image_reference'].get('publisher'),
Expand Down
10 changes: 10 additions & 0 deletions plugins/modules/azure_rm_virtualmachine_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@
type: str
returned: when created from custom image
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/myImage
community_gallery_image_id:
description:
- The community gallery image unique id for vm deployment.
type: str
returned: when created from community gallery image
sample: "/CommunityGalleries/yellowbrick-fc7e81f1-87dd-4989-9ca8-03743762e873/Images/Ubuntu-5.15.0-1035-azure_22.04"
location:
description:
- Resource location.
Expand Down Expand Up @@ -540,6 +546,10 @@ def serialize_vm(self, vm):
'offer': image['offer'],
'version': image['version']
}
elif image.get('community_gallery_image_id') is not None:
new_result['image'] = {
'community_gallery_image_id': image.get('community_gallery_image_id')
}
else:
new_result['image'] = {
'id': image.get('id', None)
Expand Down
33 changes: 33 additions & 0 deletions plugins/modules/azure_rm_virtualmachinescaleset.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@
- If a dict with the keys I(name) and I(resource_group), the image is sourced from a custom image based on the I(name) and I(resource_group) set.
Note that the key I(resource_group) is optional and if omitted, all images in the subscription will be searched for by I(name).
- Custom image support was added in Ansible 2.5.
- For community images, a dict with the keys I(community_gallery_image_id). Specified the community gallery image unique id for vm demployment.
- The I(community_gallery_image_id) can be fetched from shared gallery image GET call.
- Required when creating.
type: raw
os_disk_caching:
Expand Down Expand Up @@ -449,6 +451,32 @@
managed_disk_type: Standard_LRS
image: customimage001
- name: Create a VMSS with a community gallery image ID
azure_rm_virtualmachinescaleset:
resource_group: myResourceGroup
name: testvmss
vm_size: Standard_D8_v4
admin_username: testuser
single_placement_group: false
platform_fault_domain_count: 1
public_ip_per_vm: true
ssh_password_enabled: false
ssh_public_keys:
- path: /home/testuser/.ssh/authorized_keys
key_data: "ssh-rsa ********* xiuxi.sun@qq.com"
virtual_network_name: VMSStestVnet
subnet_name: VMSStestSubnet
managed_disk_type: Standard_LRS
orchestration_mode: Flexible
os_disk_caching: ReadWrite
image:
community_gallery_image_id: "/CommunityGalleries/yellowbrick-fc7e81f1-87dd-4989-9ca8-03743762e873/Images/Ubuntu-5.15.0-1035-azure_22.04"
data_disks:
- lun: 0
disk_size_gb: 64
caching: ReadWrite
managed_disk_type: Standard_LRS
- name: Create a VMSS with over 100 instances
azure_rm_virtualmachinescaleset:
resource_group: myResourceGroup
Expand Down Expand Up @@ -899,6 +927,11 @@ def exec_module(self, **kwargs):
image_reference = self.compute_models.ImageReference(id=self.image['id'])
except Exception as exc:
self.fail("id Error: Cannot get image from the reference id - {0}".format(self.image['id']))
elif self.image.get('community_gallery_image_id'):
try:
image_reference = self.compute_models.ImageReference(community_gallery_image_id=self.image['community_gallery_image_id'])
except Exception as exc:
self.fail("id Error: Cannot get image from the cummunity gallery image id- {0}".format(self.image['community_gallery_image_id']))
else:
self.fail("parameter error: expecting image to contain [publisher, offer, sku, version], [name, resource_group] or [id]")
elif self.image and isinstance(self.image, str):
Expand Down
20 changes: 16 additions & 4 deletions plugins/modules/azure_rm_virtualmachinescaleset_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,39 @@
offer:
description:
- The offer of the platform image or marketplace image used to create the virtual machine.
returned: always
returned: when created from marketplace image
type: str
sample: RHEL
publisher:
description:
- Publisher name.
returned: always
returned: when created from marketplace image
type: str
sample: RedHat
sku:
description:
- SKU name.
returned: always
returned: when created from marketplace image
type: str
sample: 7-RAW
version:
description:
- Image version.
returned: always
returned: when created from marketplace image
type: str
sample: 7.5.2018050901
id:
description:
- Custom image resource ID.
type: str
returned: when created from custom image
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myResourceGroup/providers/Microsoft.Compute/images/myImage
community_gallery_image_id:
description:
- The community gallery image unique id for vm deployment.
type: str
returned: when created from community gallery image
sample: "/CommunityGalleries/yellowbrick-fc7e81f1-87dd-4989-9ca8-03743762e873/Images/Ubuntu-5.15.0-1035-azure_22.04"
load_balancer:
description:
- Load balancer name.
Expand Down

0 comments on commit 78ada32

Please sign in to comment.