-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebian-on-libvirt.yml
144 lines (119 loc) · 4.25 KB
/
debian-on-libvirt.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
---
- name: Provision a CentOS VM on libvirt
hosts: vm
user: root
gather_facts: no
vars:
vm_ipaddr: "{{ hostvars[inventory_hostname]['ansible_host'] }}"
vm_netmask: 255.255.255.0
vm_gateway: 192.168.23.1
vm_dns: 192.168.23.1
vm_base_url: http://192.168.23.1/ywww/ks/
vm_repo_name: rhel69
rhel_version: "6"
force_create: false
pxe_boot: false
tasks:
- delegate_to: "{{ on_host }}"
block:
- name: Set Debian boot option
set_fact:
vm_bootopts: "debian-installer/language=en debian-installer/country=CN debian-installer/locale=en_US.UTF-8 keyboard-configuration/xkb-keymap=us netcfg/disable_autoconfig=true netcfg/disable_dhcp=true netcfg/no_default_route=true netcfg/get_hostname={{ inventory_hostname }} netcfg/get_domain= netcfg/get_ipaddress={{ vm_ipaddr }} netcfg/get_netmask={{ vm_netmask }} netcfg/get_gateway={{ vm_gateway }} netcfg/get_nameservers={{ vm_dns }} netcfg/confirm_static=true DEBCONF_DEBUG=5 vga=788 url={{ vm_base_url }}/{{ inventory_hostname }}.ps "
#vm_bootopts: "auto=true priority=critical vga=788 url={{ vm_base_url }}/{{ inventory_hostname }}.ps "
- name: Stop virtual machine
command: virsh destroy "{{ inventory_hostname }}"
register: commandresult
ignore_errors: True
when: force_create == "true"
tags: removevm
- name: Undefine virtual machine
virt_guest: guest={{ inventory_hostname }} state=absent
when: force_create == "true"
ignore_errors: True
tags: removevm
- name: Delete virtual machine storage
file:
path: "/y/vm/{{ inventory_hostname }}.qcow2"
state: absent
when: force_create == "true"
tags: removevm
ignore_errors: True
- name: Allocate storage for the VM
qemu_img:
dest: "/y/vm/{{ inventory_hostname }}.qcow2"
size: 20480
- name: Create a VM definition
template:
src: templates/vm.xml
dest: "/tmp/{{ inventory_hostname }}.xml"
- name: Create the VM
virt_guest:
guest: "{{ inventory_hostname }}"
src: "/tmp/{{ inventory_hostname }}.xml"
register: guests
- name: Create a group of unprovisioned systems
group_by:
key: "{{ guests.provisioning_status }}"
- name: Create VM ks files
template:
src: "templates/debian9.ps"
dest: "/y/www/ks/{{ inventory_hostname }}.ps"
- name: Boot the VM using the PXE images
virt_boot:
guest: "{{ inventory_hostname }}"
kernel: "/y/www/debian/stable/linux"
initrd: "/y/www/debian/stable/initrd.gz"
cmdline: "{{ vm_bootopts }}"
when:
pxe_boot != "true"
- block:
- name: Create dnsmasq directory
file:
path: "/tmp/dnsmasq/tftp/pxelinux.cfg"
state: directory
- name: Generate dnsmasq configuration file
template:
src: "templates/dnsmasq.conf.t"
dest: "/tmp/dnsmasq/dnsmasq.conf"
- name: Generate default boot file
template:
src: "templates/pxedefault"
dest: "/tmp/dnsmasq/tftp/pxelinux.cfg/default"
- name: Copy pexlinux.0
copy:
src: "templates/pxelinux.0"
dest: "/tmp/dnsmasq/tftp/pxelinux.0"
- name: Copy vmlinuz file
copy:
src: "/y/www/yum/{{ vm_repo_name }}/images/pxeboot/vmlinuz"
dest: "/tmp/dnsmasq/tftp/vmlinuz"
- name: Copy pexlinux.0
copy:
src: "/y/www/yum/{{ vm_repo_name }}/images/pxeboot/initrd.img"
dest: "/tmp/dnsmasq/tftp/initrd.img"
- name: Boot the VM form network
virt_boot:
guest: "{{ inventory_hostname }}"
boot: network
when:
pxe_boot == "true"
- name: Wait until the VM stops
virt:
name: "{{ inventory_hostname }}"
command: status
register: result
until: "result.status == 'shutdown'"
delay: 10
retries: 120
- name: Start the VM
virt_boot:
guest: "{{ inventory_hostname }}"
boot: hd
- name: Remove ks file
file:
path: "/y/www/ks/{{ inventory_hostname }}.ks"
state: absent
- name: Remove ks file
file:
path: "/tmp/{{ inventory_hostname }}.xml"
state: absent