ansible / ansible

Ansible is a radically simple IT automation platform that makes your applications and systems easier to deploy and maintain. Automate everything from code deployment to network configuration to cloud management, in a language that approaches plain English, using SSH, with no agents to install on remote systems. https://docs.ansible.com.
https://www.ansible.com/
GNU General Public License v3.0
61.84k stars 23.77k forks source link

azure_rm_virtualmachine: state:absent does not support managed disks #29662

Closed elaurijssens closed 6 years ago

elaurijssens commented 6 years ago
ISSUE TYPE
COMPONENT NAME

azure_rm_virtualmachine

ANSIBLE VERSION
ansible 2.4.0
  config file = /home/elaurijssens/voyager-image-factory/ansible.cfg
  configured module search path = [u'/home/elaurijssens/voyager-image-factory/library', u'/root/.ansible/library', u'/usr/share/ansible/library']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible-2.4.0-py2.7.egg/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Nov  6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
CONFIGURATION

None

OS / ENVIRONMENT

CentOS 7.3 - N/A

SUMMARY

Running azure_rm_virtualmachine fails if the VM has managed disks

STEPS TO REPRODUCE
    - name: Clean up VM
      azure_rm_virtualmachine:
        state: absent
        resource_group: MyRG
        client_id: "{{ secrets.client_id }}"
        secret: "{{ secrets.client_secret }}"
        subscription_id: "{{ secrets.subscription_id }}"
        tenant: "{{ secrets.tenant_id }}"
        name: "win2016"
        remove_on_absent: all
        vm_size: "{{ base_templates[cloud_type][template].vm_size }}"
EXPECTED RESULTS

VM "win2016" and associated resources are deleted from the resource group

ACTUAL RESULTS
TASK [Clean up VM] **************************************************************************************************************************
task path: /home/elaurijssens/voyager-image-factory/playbooks/azure/vm_delete.yml:19
Using module file /usr/lib/python2.7/site-packages/ansible-2.4.0-py2.7.egg/ansible/modules/cloud/azure/azure_rm_virtualmachine.py
<localhost> ESTABLISH LOCAL CONNECTION FOR USER: root
<localhost> EXEC /bin/sh -c 'echo ~ && sleep 0'
<localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775 `" && echo ansible-tmp-1505157460.96-218311556974775="` echo /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775 `" ) && sleep 0'
<localhost> PUT /tmp/tmpb9uhEf TO /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775/azure_rm_virtualmachine.py
<localhost> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775/ /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775/azure_rm_virtualmachine.py && sleep 0'
<localhost> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775/azure_rm_virtualmachine.py; rm -rf "/root/.ansible/tmp/ansible-tmp-1505157460.96-218311556974775/" > /dev/null 2>&1 && sleep 0'
The full traceback is:
Traceback (most recent call last):
  File "/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py", line 1524, in <module>
    main()
  File "/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py", line 1521, in main
    AzureRMVirtualMachine()
  File "/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py", line 652, in __init__
    supports_check_mode=True)
  File "/tmp/ansible_QSCsG6/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py", line 255, in __init__
  File "/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py", line 1074, in exec_module
    self.delete_vm(vm)
  File "/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py", line 1191, in delete_vm
    vhd_uris.append(vm.storage_profile.os_disk.vhd.uri)
AttributeError: 'NoneType' object has no attribute 'uri'

fatal: [localhost]: FAILED! => {
    "changed": false,
    "failed": true,
    "module_stderr": "Traceback (most recent call last):\n  File \"/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py\", line 1524, in <module>\n    main()\n  File \"/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py\", line 1521, in main\n    AzureRMVirtualMachine()\n  File \"/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py\", line 652, in __init__\n    supports_check_mode=True)\n  File \"/tmp/ansible_QSCsG6/ansible_modlib.zip/ansible/module_utils/azure_rm_common.py\", line 255, in __init__\n  File \"/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py\", line 1074, in exec_module\n    self.delete_vm(vm)\n  File \"/tmp/ansible_QSCsG6/ansible_module_azure_rm_virtualmachine.py\", line 1191, in delete_vm\n    vhd_uris.append(vm.storage_profile.os_disk.vhd.uri)\nAttributeError: 'NoneType' object has no attribute 'uri'\n",
    "module_stdout": "",
    "msg": "MODULE FAILURE",
    "rc": 0
}
REMARKS
elaurijssens commented 6 years ago

For the sake of completeness, except for one, all versions are default dependencies when installing azure==2.0.0. The exception is azure-mgmt-compute==2.1.0 (needed for a custom module for capturing an image of the managed VM):

# pip freeze | grep azure
azure==2.0.0
azure-batch==3.0.0
azure-common==1.1.8
azure-datalake-store==0.0.15
azure-graphrbac==0.30.0
azure-keyvault==0.3.6
azure-mgmt==1.0.0
azure-mgmt-authorization==0.30.0
azure-mgmt-batch==4.0.0
azure-mgmt-cdn==0.30.3
azure-mgmt-cognitiveservices==1.0.0
azure-mgmt-compute==2.1.0
azure-mgmt-containerregistry==0.2.1
azure-mgmt-containerservice==1.0.0
azure-mgmt-datalake-analytics==0.1.6
azure-mgmt-datalake-nspkg==2.0.0
azure-mgmt-datalake-store==0.1.6
azure-mgmt-devtestlabs==2.0.0
azure-mgmt-dns==1.0.1
azure-mgmt-documentdb==0.1.3
azure-mgmt-iothub==0.2.2
azure-mgmt-keyvault==0.31.0
azure-mgmt-logic==2.1.0
azure-mgmt-monitor==0.2.1
azure-mgmt-network==1.0.0
azure-mgmt-nspkg==2.0.0
azure-mgmt-rdbms==0.1.0
azure-mgmt-redis==4.1.0
azure-mgmt-resource==1.1.0
azure-mgmt-scheduler==1.1.2
azure-mgmt-sql==0.5.3
azure-mgmt-storage==1.0.0
azure-mgmt-trafficmanager==0.30.0
azure-mgmt-web==0.32.0
azure-nspkg==2.0.0
azure-servicebus==0.21.1
azure-servicefabric==5.6.130
azure-servicemanagement-legacy==0.20.6
azure-storage==0.34.3
msrestazure==0.4.13
ansibot commented 6 years ago

cc @haroldwongms @nitzmahone @tstringer click here for bot help

nitzmahone commented 6 years ago

Hrm, I'd searched before filing #30194 but didn't see this. Yeah, it's a legit issue: managed disks are in a different place in the storage profile and have to be deleted in a different way. Fix incoming, should be in place for 2.4.0rc4 this weekend. I also hit the extraneous requirement for vm_size- planning on nuking that too.