forked from Azure-Samples/ansible-playbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vm_create_image.yml
80 lines (70 loc) · 2.42 KB
/
vm_create_image.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Description
# ===========
# This plabyook covers scenario as described below:
# 1. create a minimal VM
# 1. set up httpd using azure_rm_virtualmachine_extension
# 1. create image from that VM
# Note: Ansible 2.7 is required for this sample.
- name: Capture VM Image with httpd set up on Ubuntu
hosts: localhost
connection: local
vars:
resource_group: "{{ resource_group_name }}"
vm_name: testvm
vm_image_name: testvmimage
location: eastus
admin_username: azureuser
admin_password: Password123!!!
tasks:
- name: Create a resource group for vnet
azure_rm_resourcegroup:
name: "{{ resource_group }}"
location: "{{ location }}"
- name: Create virtual network
azure_rm_virtualnetwork:
resource_group: "{{ resource_group }}"
name: testVnet
address_prefixes: "10.0.0.0/16"
- name: Add subnet
azure_rm_subnet:
resource_group: "{{ resource_group }}"
name: testSubnet
address_prefix: "10.0.1.0/24"
virtual_network: testVnet
- name: Create minimal VM with defaults
azure_rm_virtualmachine:
resource_group: "{{ resource_group }}"
name: "{{ vm_name }}"
admin_username: "{{ admin_username }}"
admin_password: "{{ admin_password }}"
vm_size: Standard_B1ms
image:
offer: UbuntuServer
publisher: Canonical
sku: 16.04-LTS
version: latest
- name: Create VM Extension
azure_rm_virtualmachineextension:
resource_group: "{{ resource_group }}"
name: testVMExtension
virtual_machine_name: "{{ vm_name }}"
publisher: Microsoft.Azure.Extensions
virtual_machine_extension_type: CustomScript
type_handler_version: "2.0"
auto_upgrade_minor_version: true
settings: {"commandToExecute": "sudo apt-get -y install apache2"}
- name: Stop VM before it can be generalized and image captured
azure_rm_virtualmachine:
resource_group: "{{ resource_group }}"
name: "{{ vm_name }}"
started: no
- name: Generalize VM using version >= 2.8
azure_rm_virtualmachine:
resource_group: "{{ resource_group }}"
name: "{{ vm_name }}"
generalized: true
- name: Create an image from a virtual machine
azure_rm_image:
resource_group: "{{ resource_group }}"
name: "{{ vm_image_name }}"
source: "{{ vm_name }}"