2017-12-20 22:05:36 +01:00
|
|
|
- name: Create storage account name
|
|
|
|
set_fact:
|
2018-06-12 04:52:07 +02:00
|
|
|
vm_name: "vm{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}x"
|
|
|
|
storage_name: "st{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
|
|
|
storage_container_name: "sc{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
|
|
|
public_ip_name: "pip{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
|
|
|
security_group_name: "sg{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
|
|
|
blob_name: "blob{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
2018-12-05 19:40:19 +01:00
|
|
|
empty_disk_name: "emptydisk{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
|
|
|
|
- name: Create storage account
|
|
|
|
azure_rm_storageaccount:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
name: "{{ storage_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
account_type: Standard_LRS
|
|
|
|
|
|
|
|
- name: Create virtual network
|
|
|
|
azure_rm_virtualnetwork:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
name: "{{ vm_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
address_prefixes: "10.10.0.0/16"
|
|
|
|
|
|
|
|
- name: Add subnet
|
|
|
|
azure_rm_subnet:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
name: "{{ vm_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
address_prefix: "10.10.0.0/24"
|
2018-06-12 04:52:07 +02:00
|
|
|
virtual_network: "{{ vm_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
|
|
|
|
- name: Create public ip
|
|
|
|
azure_rm_publicipaddress:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
allocation_method: Static
|
2018-06-12 04:52:07 +02:00
|
|
|
name: "{{ public_ip_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
|
|
|
|
- name: Create security group
|
|
|
|
azure_rm_securitygroup:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
name: "{{ security_group_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
|
|
|
|
- name: Create NIC
|
|
|
|
azure_rm_networkinterface:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
name: "{{ vm_name }}"
|
|
|
|
virtual_network: "{{ vm_name }}"
|
|
|
|
subnet: "{{ vm_name }}"
|
|
|
|
public_ip_name: "{{ public_ip_name }}"
|
|
|
|
security_group: "{{ security_group_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
|
|
|
|
- name: Create virtual machine
|
|
|
|
azure_rm_virtualmachine:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
name: "{{ vm_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
vm_size: Standard_A0
|
2018-06-12 04:52:07 +02:00
|
|
|
storage_account: "{{ storage_name }}"
|
|
|
|
storage_container: "{{ storage_container_name }}"
|
|
|
|
storage_blob: "{{ blob_name }}.vhd"
|
2017-12-20 22:05:36 +01:00
|
|
|
admin_username: adminuser
|
|
|
|
admin_password: Password123!
|
|
|
|
os_type: Linux
|
2018-06-12 04:52:07 +02:00
|
|
|
network_interfaces: "{{ vm_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
image:
|
|
|
|
offer: UbuntuServer
|
|
|
|
publisher: Canonical
|
|
|
|
sku: 16.04-LTS
|
|
|
|
version: latest
|
|
|
|
|
|
|
|
- name: Deallocate the virtual machine
|
|
|
|
azure_rm_virtualmachine:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
name: "{{ vm_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
allocated: no
|
|
|
|
vm_size: Standard_A0
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- name: Start the virtual machine
|
|
|
|
azure_rm_virtualmachine:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
name: "{{ vm_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
vm_size: Standard_A0
|
|
|
|
|
2018-12-05 19:40:19 +01:00
|
|
|
- name: Create new empty managed disk
|
|
|
|
azure_rm_managed_disk:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: "{{ empty_disk_name }}"
|
|
|
|
storage_account_type: "Standard_LRS"
|
|
|
|
disk_size_gb: 1
|
|
|
|
register: emptydisk
|
|
|
|
|
2017-12-20 22:05:36 +01:00
|
|
|
- name: Create an image from VM (check mode)
|
|
|
|
azure_rm_image:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
source: "https://{{ storage_name }}.blob.core.windows.net/{{ storage_container_name }}/{{ blob_name }}.vhd"
|
2017-12-20 22:05:36 +01:00
|
|
|
name: testimage001
|
|
|
|
os_type: Linux
|
2018-12-05 19:40:19 +01:00
|
|
|
data_disk_sources:
|
|
|
|
- "{{ empty_disk_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
check_mode: yes
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that: output.changed
|
|
|
|
|
|
|
|
- name: Create an image from VM
|
|
|
|
azure_rm_image:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
source: "https://{{ storage_name }}.blob.core.windows.net/{{ storage_container_name }}/{{ blob_name }}.vhd"
|
2017-12-20 22:05:36 +01:00
|
|
|
name: testimage001
|
|
|
|
os_type: Linux
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- output.changed
|
|
|
|
- output.id
|
|
|
|
|
|
|
|
- name: Create an image from VM (idempotent)
|
|
|
|
azure_rm_image:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
source: "https://{{ storage_name }}.blob.core.windows.net/{{ storage_container_name }}/{{ blob_name }}.vhd"
|
2017-12-20 22:05:36 +01:00
|
|
|
name: testimage001
|
|
|
|
os_type: Linux
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- not output.changed
|
|
|
|
- output.id
|
|
|
|
|
|
|
|
- name: Delete image (check mode)
|
|
|
|
azure_rm_image:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: testimage001
|
|
|
|
state: absent
|
|
|
|
register: output
|
|
|
|
check_mode: yes
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- output.changed
|
|
|
|
|
|
|
|
- name: Delete image
|
|
|
|
azure_rm_image:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: testimage001
|
|
|
|
state: absent
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- output.changed
|
|
|
|
|
|
|
|
- name: Delete image (idempotent)
|
|
|
|
azure_rm_image:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: testimage001
|
|
|
|
state: absent
|
|
|
|
register: output
|
|
|
|
|
|
|
|
- assert:
|
|
|
|
that:
|
|
|
|
- not output.changed
|
|
|
|
|
2018-12-05 19:40:19 +01:00
|
|
|
- name: Delete empty disk
|
|
|
|
azure_rm_managed_disk:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
name: "{{ empty_disk_name }}"
|
|
|
|
state: absent
|
|
|
|
|
2017-12-20 22:05:36 +01:00
|
|
|
- name: Delete VM
|
|
|
|
azure_rm_virtualmachine:
|
|
|
|
resource_group: "{{ resource_group }}"
|
2018-06-12 04:52:07 +02:00
|
|
|
name: "{{ vm_name }}"
|
2017-12-20 22:05:36 +01:00
|
|
|
state: absent
|
|
|
|
vm_size: Standard_A0
|
|
|
|
register: output
|
2018-06-12 04:52:07 +02:00
|
|
|
|
|
|
|
- name: Delete public ip
|
|
|
|
azure_rm_publicipaddress:
|
|
|
|
resource_group: "{{ resource_group }}"
|
|
|
|
allocation_method: Static
|
|
|
|
name: "{{ public_ip_name }}"
|
|
|
|
state: absent
|