ansible-collections / azure

Development area for Azure Collections
https://galaxy.ansible.com/azure/azcollection
GNU General Public License v3.0
247 stars 330 forks source link

managed_disk_type cannot be set when creating a VM from custom image #883

Open ValioSV opened 2 years ago

ValioSV commented 2 years ago
SUMMARY

I cannot set "managed_disk_type" property to "StandardSSD_LRS" when creating a VM from custom image.

ISSUE TYPE
COMPONENT NAME

azure.azcollection.azure_rm_virtualmachine module > managed_disk_type

ANSIBLE VERSION
  config file = None
  configured module search path = ['/home/lroot/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.10/site-packages/ansible
  ansible collection location = /home/lroot/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.10.4 (main, Mar 26 2022, 21:00:27) [GCC]
  jinja version = 3.1.1
  libyaml = True
COLLECTION VERSION
# /usr/lib/python3.10/site-packages/ansible_collections
Collection         Version
------------------ -------
azure.azcollection 1.12.0
CONFIGURATION
DISPLAY_SKIPPED_HOSTS(/tmp/valio/ansible.cfg) = False
HOST_KEY_CHECKING(/tmp/valio/ansible.cfg) = False
INTERPRETER_PYTHON(/tmp/valio/ansible.cfg) = auto_legacy_silent
OS / ENVIRONMENT

SUSE Linux Enterprise Server 15 SP2

STEPS TO REPRODUCE

I am using ansible-playbook and roles. Below is the code of the tasks for creating a VM:

Role (create-vm-azure):

- name: Import VM-vars json file
  include_vars:
    file: "sysconfigs/azure/vm-vars.json"
    name: vm_vars

- name: Set all VM variables
  set_fact:
    res_group: "{{ vm_vars | json_query(res_group) }}"
    virtual_network: "{{ vm_vars | json_query(virtual_network) }}"
    subnet_name: "{{ vm_vars | json_query(subnet_name) }}"
    security_group: "{{ vm_vars | json_query(security_group) }}"
    admin_username: "{{ vm_vars | json_query(admin_username) }}"
    managed_disk_type: "{{ vm_vars | json_query(managed_disk_type) }}"
    location: "{{ vm_vars | json_query(location) }}"
    image_id: "{{ vm_vars | json_query(image_id) }}"
    vm_size: "{{ vm_vars | json_query(vm_size) }}"

- name: Create network interface for the VM
  azure_rm_networkinterface:
    name: "{{ network_interface_name }}"
    resource_group: "{{ res_group }}"
    virtual_network: "{{ virtual_network }}"
    subnet_name: "{{ subnet_name }}"
    security_group: "{{ security_group }}"
    ip_configurations:
      - name: ipconfig1
        primary: True
    tags:
      vm_name: "{{ vm_name }}"
  register: ip_address

- name: Create VM
  azure_rm_virtualmachine:
    resource_group: "{{ res_group }}"
    name: "{{ vm_name }}"
    vm_size: "{{ vm_size }}"
    admin_username: "{{ admin_username }}"
    ssh_password_enabled: false
    ssh_public_keys:
      - path: /home/lroot/.ssh/authorized_keys
        key_data: "{{ ssh_key }}"
    managed_disk_type: "{{ managed_disk_type }}"
    network_interfaces: "{{ network_interface_name }}"
    virtual_network_name: "{{ virtual_network }}"
    public_ip_allocation_method: Disabled
    location: "{{ location }}"
    image:
      id: "{{ image_id }}"
    tags:
      vm_name: "{{ vm_name }}"
    data_disks:
      - lun: 0
        managed_disk_type : StandardSSD_LRS
      - lun: 1
        managed_disk_type : StandardSSD_LRS
      - lun: 2
        managed_disk_type: StandardSSD_LRS
      - lun: 3
        managed_disk_type: StandardSSD_LRS

Playbook:

- name: Provision VM
  hosts: localhost
  connection: local
  collections:
    - azure.azcollection
  vars:
    network_interface_name: "{{ vm_name }}-nic"
    res_group: "resource_group.{{ env }}.name"
    virtual_network: "resource_group.{{ env }}.virtual_network"
    subnet_name: "resource_group.{{ env }}.subnet_name"
    security_group: "resource_group.{{ env }}.security_group"
    admin_username: "resource_group.{{ env }}.admin_username"
    managed_disk_type: "resource_group.{{ env }}.managed_disk_type"
    location: "resource_group.{{ env }}.location"
    image_id: "type.{{ vm_type }}.image_id"
    vm_size: "type.{{ vm_type }}.vm_size"
  roles:
    - create-vm-azure
EXPECTED RESULTS

The VM is created with data disks type "StandardSSD_LRS"

ACTUAL RESULTS

The VM is created with data disks type "Premium_SDD"

Fred-sun commented 2 years ago

@ValioSV I tested it locally, your Playbook is missing the required parameter "disk_size_gb ", and I got a VM disk type of" StandardSSD_LRS ". Can you try again here? And share the entire testing process, which will help solve any problems you encounter! Thank you very much!.


                "data_disks": [
                    {
                        "caching": "ReadOnly",
                        "disk_size_gb": 128,
                        "lun": 0,
                        "managed_disk_id": "/subscriptions/xxxx/resourceGroups/v-xxxx/providers/Microsoft.Compute/disks/testvm02-datadisk-0",
                        "managed_disk_type": "StandardSSD_LRS",
                        "name": "testvm02-datadisk-0"
                    }
                ]
ValioSV commented 2 years ago

@Fred-sun I will try with the parameter "name" and "caching" added. I already tried with parameter "disk_size_gb" and with "create_option: FromImage" but it fails. The thing here is that I am creating a new VM from template and the LUNs are predefined in the Azure template so I do not have the "managed_disk_id". In the "Create VM" task above I am specifying: image: id: "{{ image_id }}" I tried many variations but it never sets the LUN to "StandardSSD_LRS". If you create a new data disk upfront and then attach it to the VM probably it works but this is not what I need.

Fred-sun commented 2 years ago

@ValioSV Sorry, I still don't understand what you mean. Do you want to mount the existing disk to the VM?

ValioSV commented 2 years ago

@Fred-sun No I do not want to mount existing disks. I have a custom template created in Azure cloud and I am using it to deploy the VM using "azure_rm_virtualmachine". This custom template has 4 additional data disks (LUNs) already created and configured. So when I call the deploy from template in ansible it simply creates a new machine with one OS disk and 4 LUNs. The problem is that the LUNs are of type "Premium_SDD" and not "StandardSSD_LRS".

Fred-sun commented 2 years ago

@ValioSV But from your description above, there is no mention of templates? Can you provide templates and Playbook? Will this help solve the problem you are having? Thank you very much!

ValioSV commented 2 years ago

@Fred-sun It is all described in the summary of the issue: "I cannot set "managed_disk_type" property to "StandardSSD_LRS" when creating a VM from custom image." See above. I have already provided the ansible role(create-vm-azure) above. Now I added the playbook too. See above in the description of the issue. Unfortunately I cannot provide the custom image from Azure because of company policies. I do not think the custom image makes any difference. Probably this will be reproduced with any custom image that you may have available.

Fred-sun commented 2 years ago

@ValioSV Understand your problem! Now, when we create a VM using "custom image", it will be created according to the VM configuration of the image. we cannot create other disks when we create it. But at the end of creation, we can add the required data disks to the VM via the "azure_rm_mangeddisk.py" module! Thank you very much!


like this:
    - name: Attach new disk to VM
      azure_rm_manageddisk:
        resource_group: "{{ resource_group }}"
        name: "{{ new_disk_name }}"
        disk_size_gb: 1024
        storage_account_type: StandardSSD_LRS
        managed_by:  "{{ vm_name }}"
ValioSV commented 2 years ago

@Fred-sun I have tried that and it does not work. In addition, I do not want to create a new disk for the VM but I want to change the type from the custom image while deploying a new instance. I did a little experiment. I created a new VM with one disk. Then I tried to change its type using ansible like this:

- name: Test data disks
  hosts: localhost
  connection: local
  vars:
    resource_group: LamaDevLandscape
    disk_id: testvm1_OsDisk_1_1abea8cf190e4ebcab3fdd020fd311eb
    vm_name: testvm1
  collections:
    - azure.azcollection
  tasks:
    - name: Detach the disk from the VM
      azure_rm_manageddisk:
        name: "{{ disk_id }}"
        location: westeurope
        resource_group: "{{ resource_group }}"
        managed_by: "{{ vm_name }}"

    - name: Change data disks type
      azure_rm_manageddisk:
        name: "{{ disk_id }}"
        location: westeurope
        resource_group: "{{ resource_group }}"
        state: present
        storage_account_type: StandardSSD_LRS
        disk_size_gb: 30
        managed_by: "{{ vm_name }}"

    - name: Attach the disk to the VM
      azure_rm_manageddisk:
        name: "{{ disk_id }}"
        location: westeurope
        resource_group: "{{ resource_group }}"
        attach_caching: read_only
        disk_size_gb: 64
        lun: 0
        state: present
        managed_by: "{{ vm_name }}"

It fails with the following error:

<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/lroot/.ansible/tmp/ansible-tmp-1662641019.7764714-25633-21163196332198/ > /dev/null 2>&1 && sleep 0' The full traceback is: File "/tmp/ansible_azure_rm_manageddisk_payloadzyxpw3v/ansible_azure_rm_manageddisk_payload.zip/ansible_collections/azure/azcollection/plugins/modules/azure_rm_manageddisk.py", line 470, in create_or_update_managed_disk File "/home/lroot/.local/lib/python3.10/site-packages/azure/core/tracing/decorator.py", line 73, in wrapper_use_tracer return func(*args, **kwargs) File "/home/lroot/.local/lib/python3.10/site-packages/azure/mgmt/compute/v2021_04_01/operations/_disks_operations.py", line 430, in begin_create_or_update raw_result = self._create_or_update_initial( File "/home/lroot/.local/lib/python3.10/site-packages/azure/mgmt/compute/v2021_04_01/operations/_disks_operations.py", line 374, in _create_or_update_initial map_error(status_code=response.status_code, response=response, error_map=error_map) File "/home/lroot/.local/lib/python3.10/site-packages/azure/core/exceptions.py", line 105, in map_error raise error fatal: [localhost]: FAILED! => { "changed": false, "invocation": { "module_args": { "ad_user": null, "adfs_authority_url": null, "api_profile": "latest", "append_tags": true, "attach_caching": null, "auth_source": "auto", "cert_validation_mode": null, "client_id": null, "cloud_environment": "AzureCloud", "create_option": null, "disk_size_gb": 30, "location": "westeurope", "log_mode": null, "log_path": null, "lun": null, "managed_by": "testvm1", "name": "testvm1_OsDisk_1_1abea8cf190e4ebcab3fdd020fd311eb", "os_type": null, "password": null, "profile": null, "resource_group": "LamaDevLandscape", "secret": null, "source_uri": null, "state": "present", "storage_account_type": "StandardSSD_LRS", "subscription_id": null, "tags": null, "tenant": null, "zone": null } }, "msg": "Error creating the managed disk: (OperationNotAllowed) Cannot change account type, fault domain or network spine of disk testvm1_OsDisk_1_1abea8cf190e4ebcab3fdd020fd311eb while it is attached to running VM /subscriptions/49e39056-98af-4714-9d1d-7376dd148a23/resourceGroups/LamaDevLandscape/providers/Microsoft.Compute/virtualMachines/testvm1.\nCode: OperationNotAllowed\nMessage: Cannot change account type, fault domain or network spine of disk testvm1_OsDisk_1_1abea8cf190e4ebcab3fdd020fd311eb while it is attached to running VM /subscriptions/49e39056-98af-4714-9d1d-7376dd148a23/resourceGroups/LamaDevLandscape/providers/Microsoft.Compute/virtualMachines/testvm1." }

Fred-sun commented 2 years ago

@ValioSV OK! I will test your idear and share result to you!

Fred-sun commented 1 year ago

@ValioSV I understand your means. Because the image cannot be parsed for the time being, it cannot set the type of data disk in the module. We will tracking it. Thank you very much!