From e5a5734051767b7cebfdf205e83da0539e1df696 Mon Sep 17 00:00:00 2001 From: haiyuan_zhang Date: Wed, 28 Apr 2021 04:54:08 +0800 Subject: [PATCH] Fixing inventory issue (#518) * fix inventory name issue. update inventory test. * refine inventory tests to cover both fqn and short name case. * enable teardown in inventory test. --- plugins/doc_fragments/azure_rm.py | 2 +- .../inventory_azure/playbooks/setup.yml | 65 ++++++++++++------- .../inventory_azure/playbooks/teardown.yml | 58 ++++++++++------- .../playbooks/test_inventory.yml | 31 +-------- .../targets/inventory_azure/runme.sh | 19 ++++-- .../inventory_azure/templates/basic2.yml | 3 + 6 files changed, 97 insertions(+), 81 deletions(-) create mode 100644 tests/integration/targets/inventory_azure/templates/basic2.yml diff --git a/plugins/doc_fragments/azure_rm.py b/plugins/doc_fragments/azure_rm.py index b96037624..60f82497e 100644 --- a/plugins/doc_fragments/azure_rm.py +++ b/plugins/doc_fragments/azure_rm.py @@ -16,7 +16,7 @@ class ModuleDocFragment(object): plugin: description: marks this as an instance of the 'azure_rm' plugin required: true - choices: ['azure_rm'] + choices: ['azure_rm', 'azure.azcollection.azure_rm'] include_vm_resource_groups: description: A list of resource group names to search for virtual machines. '\*' will include all resource groups in the subscription. diff --git a/tests/integration/targets/inventory_azure/playbooks/setup.yml b/tests/integration/targets/inventory_azure/playbooks/setup.yml index 1fed38510..e00e4bb9f 100644 --- a/tests/integration/targets/inventory_azure/playbooks/setup.yml +++ b/tests/integration/targets/inventory_azure/playbooks/setup.yml @@ -1,27 +1,48 @@ --- -- include_vars: - file: vars.yml +- hosts: localhost + connection: local + gather_facts: no -- name: SETUP | Create storage account - azure_rm_storageaccount: - resource_group: "{{ resource_group }}" - name: "{{ storage_account }}" - account_type: Standard_LRS + tasks: + - include_vars: vars.yml + - name: SETUP | Create storage account + azure_rm_storageaccount: + resource_group: "{{ resource_group }}" + name: "{{ storage_account }}" + account_type: Standard_LRS -- name: SETUP | Create availability set - azure_rm_availabilityset: - name: "{{ availability_set }}" - resource_group: "{{ resource_group }}" + - name: SETUP | Create availability set + azure_rm_availabilityset: + name: "{{ availability_set }}" + resource_group: "{{ resource_group }}" -- name: SETUP | Create virtual network - azure_rm_virtualnetwork: - resource_group: "{{ resource_group }}" - name: "{{ network_name }}" - address_prefixes: "{{ network }}" + - name: SETUP | Create virtual network + azure_rm_virtualnetwork: + resource_group: "{{ resource_group }}" + name: "{{ network_name }}" + address_prefixes: "{{ network }}" -- name: SETUP | Create subnet - azure_rm_subnet: - resource_group: "{{ resource_group }}" - name: "{{ subnet_name }}" - address_prefix: "{{ subnet }}" - virtual_network: "{{ network_name }}" \ No newline at end of file + - name: SETUP | Create subnet + azure_rm_subnet: + resource_group: "{{ resource_group }}" + name: "{{ subnet_name }}" + address_prefix: "{{ subnet }}" + virtual_network: "{{ network_name }}" + + - name: Create minimal VM with defaults + azure_rm_virtualmachine: + resource_group: "{{ resource_group }}" + name: "{{ vm_name }}" + admin_username: testuser + ssh_password_enabled: false + ssh_public_keys: + - path: /home/testuser/.ssh/authorized_keys + key_data: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfoYlIV4lTPZTv7hXaVwQQuqBgGs4yeNRX0SPo2+HQt9u4X7IGwrtXc0nEUm6LfaCikMH58bOL8f20NTGz285kxdFHZRcBXtqmnMz2rXwhK9gwq5h1khc+GzHtdcJXsGA4y0xuaNcidcg04jxAlN/06fwb/VYwwWTVbypNC0gpGEpWckCNm8vlDlA55sU5et0SZ+J0RKVvEaweUOeNbFZqckGPA384imfeYlADppK/7eAxqfBVadVvZG8IJk4yvATgaIENIFj2cXxqu2mQ/Bp5Wr45uApvJsFXmi+v/nkiOEV1QpLOnEwAZo6EfFS4CCQtsymxJCl1PxdJ5LD4ZOtP xiuxi.sun@qq.com" + vm_size: Standard_B1ms + virtual_network: "{{ network_name }}" + image: + offer: UbuntuServer + publisher: Canonical + sku: 16.04-LTS + version: latest + register: vm_output \ No newline at end of file diff --git a/tests/integration/targets/inventory_azure/playbooks/teardown.yml b/tests/integration/targets/inventory_azure/playbooks/teardown.yml index c1c5e3ce9..3a1adfdd4 100644 --- a/tests/integration/targets/inventory_azure/playbooks/teardown.yml +++ b/tests/integration/targets/inventory_azure/playbooks/teardown.yml @@ -1,26 +1,40 @@ --- -- name: Destroy subnet - azure_rm_subnet: - resource_group: "{{ resource_group }}" - virtual_network: "{{ network_name }}" - name: "{{ subnet_name }}" - state: absent +- hosts: localhost + connection: local + gather_facts: no -- name: Destroy virtual network - azure_rm_virtualnetwork: - resource_group: "{{ resource_group }}" - name: "{{ network_name }}" - state: absent + tasks: + - include_vars: vars.yml -- name: Destroy availability set - azure_rm_availabilityset: - resource_group: "{{ resource_group }}" - name: "{{ availability_set }}" - state: absent + - name: Delete VM + azure_rm_virtualmachine: + resource_group: "{{ resource_group }}" + name: "{{ vm_name }}" + remove_on_absent: all_autocreated + state: absent -- name: Destroy storage account - azure_rm_storageaccount: - resource_group: "{{ resource_group }}" - name: "{{ storage_account }}" - force_delete_nonempty: yes - state: absent + - name: Destroy subnet + azure_rm_subnet: + resource_group: "{{ resource_group }}" + virtual_network: "{{ network_name }}" + name: "{{ subnet_name }}" + state: absent + + - name: Destroy virtual network + azure_rm_virtualnetwork: + resource_group: "{{ resource_group }}" + name: "{{ network_name }}" + state: absent + + - name: Destroy availability set + azure_rm_availabilityset: + resource_group: "{{ resource_group }}" + name: "{{ availability_set }}" + state: absent + + - name: Destroy storage account + azure_rm_storageaccount: + resource_group: "{{ resource_group }}" + name: "{{ storage_account }}" + force_delete_nonempty: yes + state: absent \ No newline at end of file diff --git a/tests/integration/targets/inventory_azure/playbooks/test_inventory.yml b/tests/integration/targets/inventory_azure/playbooks/test_inventory.yml index ddb998b70..57bef334f 100644 --- a/tests/integration/targets/inventory_azure/playbooks/test_inventory.yml +++ b/tests/integration/targets/inventory_azure/playbooks/test_inventory.yml @@ -3,26 +3,7 @@ connection: local gather_facts: no tasks: - - name: setup - include_tasks: setup.yml - - - name: Create minimal VM with defaults - azure_rm_virtualmachine: - resource_group: "{{ resource_group }}" - name: "{{ vm_name }}" - admin_username: testuser - ssh_password_enabled: false - ssh_public_keys: - - path: /home/testuser/.ssh/authorized_keys - key_data: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfoYlIV4lTPZTv7hXaVwQQuqBgGs4yeNRX0SPo2+HQt9u4X7IGwrtXc0nEUm6LfaCikMH58bOL8f20NTGz285kxdFHZRcBXtqmnMz2rXwhK9gwq5h1khc+GzHtdcJXsGA4y0xuaNcidcg04jxAlN/06fwb/VYwwWTVbypNC0gpGEpWckCNm8vlDlA55sU5et0SZ+J0RKVvEaweUOeNbFZqckGPA384imfeYlADppK/7eAxqfBVadVvZG8IJk4yvATgaIENIFj2cXxqu2mQ/Bp5Wr45uApvJsFXmi+v/nkiOEV1QpLOnEwAZo6EfFS4CCQtsymxJCl1PxdJ5LD4ZOtP xiuxi.sun@qq.com" - vm_size: Standard_B1ms - virtual_network: "{{ network_name }}" - image: - offer: UbuntuServer - publisher: Canonical - sku: 16.04-LTS - version: latest - register: vm_output + - include_vars: vars.yml - meta: refresh_inventory @@ -30,13 +11,3 @@ assert: that: - vm_name in hostvars - - - name: Delete VM - azure_rm_virtualmachine: - resource_group: "{{ resource_group }}" - name: "{{ vm_name }}" - remove_on_absent: all_autocreated - state: absent - - - name: teardown - include_tasks: teardown.yml diff --git a/tests/integration/targets/inventory_azure/runme.sh b/tests/integration/targets/inventory_azure/runme.sh index db56ed8db..6f381c0a5 100755 --- a/tests/integration/targets/inventory_azure/runme.sh +++ b/tests/integration/targets/inventory_azure/runme.sh @@ -2,16 +2,23 @@ set -eux -# ensure test config is empty +# make sure inventory is empty at the begining of the tests ansible-playbook playbooks/empty_inventory_config.yml "$@" +# create vm +ansible-playbook playbooks/setup.yml "$@" + export ANSIBLE_INVENTORY=test.azure_rm.yml -# generate inventory config and test using it -ansible-playbook playbooks/create_inventory_config.yml "$@" +# using fully qualified name +ansible-playbook playbooks/create_inventory_config.yml "$@" +ansible-playbook playbooks/test_inventory.yml "$@" + +# using short name +ansible-playbook playbooks/empty_inventory_config.yml "$@" +ansible-playbook playbooks/create_inventory_config.yml "$@" --extra-vars "template=basic2.yml" ansible-playbook playbooks/test_inventory.yml "$@" -#ansible-inventory -i test.azure_rm.yml --list -vvv --playbook-dir=./ -# cleanup inventory config -ansible-playbook playbooks/empty_inventory_config.yml "$@" \ No newline at end of file +# teardown +ansible-playbook playbooks/teardown.yml "$@" diff --git a/tests/integration/targets/inventory_azure/templates/basic2.yml b/tests/integration/targets/inventory_azure/templates/basic2.yml new file mode 100644 index 000000000..89ec2e131 --- /dev/null +++ b/tests/integration/targets/inventory_azure/templates/basic2.yml @@ -0,0 +1,3 @@ +--- +plugin: azure_rm +plain_host_names: yes \ No newline at end of file