Debian Codegraph @

ansible_collections/community/libvirt/plugins/modules/virt_install.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # (c) 2025, Joey Zhang <[email protected]>
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 from __future__ import absolute_import, division, print_function
8 __metaclass__ = type
9
10
11 DOCUMENTATION = """
12 ---
13 module: virt_install
14 version_added: 2.0.0
15 author: "Joey Zhang (@thinkdoggie)"
16 short_description: Provision new virtual machines using virt-install tool
17 description:
18 - Create and install virtual machines using C(virt-install) with a declarative configuration.
19 options:
20 name:
21 type: str
22 description:
23 - Name of the new guest virtual machine instance.
24 required: true
25 state:
26 choices: [ present, absent ]
27 type: str
28 description:
29 - If set to V(present), create the VM if it does not exist.
30 - If set to V(absent), remove the VM if it exists.
31 default: present
32 recreate:
33 type: bool
34 description:
35 - Use with present to force the re-creation of an existing VM.
36 default: false
37 extends_documentation_fragment:
38 - community.libvirt.virt.options_uri
39 - community.libvirt.virt_install.options_memory
40 - community.libvirt.virt_install.options_memorybacking
41 - community.libvirt.virt_install.options_arch
42 - community.libvirt.virt_install.options_machine
43 - community.libvirt.virt_install.options_metadata
44 - community.libvirt.virt_install.options_events
45 - community.libvirt.virt_install.options_resource
46 - community.libvirt.virt_install.options_sysinfo
47 - community.libvirt.virt_install.options_qemu_commandline
48 - community.libvirt.virt_install.options_vcpus
49 - community.libvirt.virt_install.options_numatune
50 - community.libvirt.virt_install.options_memtune
51 - community.libvirt.virt_install.options_blkiotune
52 - community.libvirt.virt_install.options_cpu
53 - community.libvirt.virt_install.options_cputune
54 - community.libvirt.virt_install.options_security
55 - community.libvirt.virt_install.options_keywrap
56 - community.libvirt.virt_install.options_iothreads
57 - community.libvirt.virt_install.options_features
58 - community.libvirt.virt_install.options_clock
59 - community.libvirt.virt_install.options_pm
60 - community.libvirt.virt_install.options_launch_security
61 - community.libvirt.virt_install.options_cdrom
62 - community.libvirt.virt_install.options_location
63 - community.libvirt.virt_install.options_pxe
64 - community.libvirt.virt_install.options_import
65 - community.libvirt.virt_install.options_extra_args
66 - community.libvirt.virt_install.options_initrd_inject
67 - community.libvirt.virt_install.options_install
68 - community.libvirt.virt_install.options_unattended
69 - community.libvirt.virt_install.options_cloud_init
70 - community.libvirt.virt_install.options_boot
71 - community.libvirt.virt_install.options_idmap
72 - community.libvirt.virt_install.options_osinfo
73 - community.libvirt.virt_install.options_disks
74 - community.libvirt.virt_install.options_filesystems
75 - community.libvirt.virt_install.options_networks
76 - community.libvirt.virt_install.options_graphics_devices
77 - community.libvirt.virt_install.options_virt_type
78 - community.libvirt.virt_install.options_hvm
79 - community.libvirt.virt_install.options_paravirt
80 - community.libvirt.virt_install.options_container
81 - community.libvirt.virt_install.options_controller_devices
82 - community.libvirt.virt_install.options_input_devices
83 - community.libvirt.virt_install.options_host_devices
84 - community.libvirt.virt_install.options_sound_devices
85 - community.libvirt.virt_install.options_audio_devices
86 - community.libvirt.virt_install.options_watchdog_devices
87 - community.libvirt.virt_install.options_serial_devices
88 - community.libvirt.virt_install.options_parallel_devices
89 - community.libvirt.virt_install.options_channel_devices
90 - community.libvirt.virt_install.options_console_devices
91 - community.libvirt.virt_install.options_video_devices
92 - community.libvirt.virt_install.options_smartcard_devices
93 - community.libvirt.virt_install.options_redirected_devices
94 - community.libvirt.virt_install.options_memballoon_devices
95 - community.libvirt.virt_install.options_tpm_devices
96 - community.libvirt.virt_install.options_rng_devices
97 - community.libvirt.virt_install.options_panic_devices
98 - community.libvirt.virt_install.options_shmem_devices
99 - community.libvirt.virt_install.options_vsock_devices
100 - community.libvirt.virt_install.options_iommu_devices
101 - community.libvirt.virt_install.options_autostart
102 - community.libvirt.virt_install.options_transient
103 - community.libvirt.virt_install.options_destroy_on_exit
104 - community.libvirt.virt_install.options_noreboot
105 - community.libvirt.requirements
106 attributes:
107 check_mode:
108 description: Supports check_mode.
109 support: full
110 requirements:
111 - "virt-install"
112 notes:
113 - The C(virt-install) command is provided by different packages on different distributions.
114 - On Debian/Ubuntu, install the C(virtinst) package.
115 - On RHEL/CentOS/Fedora and openSUSE, install the C(virt-install) package.
116 seealso:
117 - name: virt-install Man Page
118 description: Ubuntu manpage of virt-install tool.
119 link: https://manpages.ubuntu.com/manpages/focal/man1/virt-install.1.html
120 """
121
122 EXAMPLES = """
123 # Basic VM creation with Fedora installation
124 - name: Create a basic Fedora VM
125 community.libvirt.virt_install:
126 name: my-fedora-vm
127 memory: 2048
128 vcpus: 2
129 disks:
130 - size: 20
131 osinfo:
132 name: fedora39
133 location: https://download.fedoraproject.org/pub/fedora/linux/releases/39/Server/x86_64/
134 graphics:
135 type: spice
136 networks:
137 - network: default
138
139 # Windows 10 VM with CDROM installation
140 - name: Create Windows 10 VM
141 community.libvirt.virt_install:
142 name: my-win10-vm
143 memory: 4096
144 vcpus: 4
145 disks:
146 - size: 40
147 format: qcow2
148 osinfo:
149 name: win10
150 cdrom: /path/to/my/win10.iso
151 graphics:
152 type: vnc
153 password: mypassword
154 networks:
155 - network: default
156 model:
157 type: e1000
158
159 # Import existing disk image
160 - name: Import existing Debian VM
161 community.libvirt.virt_install:
162 name: my-debian-vm
163 memory: 1024
164 vcpus: 2
165 disks:
166 - path: /home/user/VMs/my-debian9.img
167 osinfo:
168 name: debian9
169 import: true
170 networks:
171 - bridge: br0
172
173 # CentOS installation with custom storage and network configuration
174 - name: Create CentOS VM with custom configuration
175 community.libvirt.virt_install:
176 name: centos-server
177 memory: 8192
178 vcpus: 8
179 disks:
180 - pool: default
181 size: 50
182 format: qcow2
183 cache: writeback
184 - pool: default
185 size: 100
186 format: qcow2
187 bus: virtio
188 osinfo:
189 name: centos7.0
190 location: http://mirror.centos.org/centos-7/7/os/x86_64/
191 extra_args: "ks=http://myserver/centos7.ks"
192 graphics:
193 type: vnc
194 listen: 0.0.0.0
195 port: 5901
196 networks:
197 - bridge: br0
198 model:
199 type: virtio
200 - network: isolated-net
201
202 # Ubuntu server with unattended installation
203 - name: Create Ubuntu server with unattended install
204 community.libvirt.virt_install:
205 name: ubuntu-server
206 memory: 2048
207 vcpus: 2
208 disks:
209 - size: 25
210 osinfo:
211 name: ubuntu20.04
212 location: http://archive.ubuntu.com/ubuntu/dists/focal/main/installer-amd64/
213 unattended:
214 profile: jeos
215 admin_password_file: /tmp/root_password
216 user_login: ansible
217 user_password_file: /tmp/user_password
218 networks:
219 - network: default
220
221 # ARM VM with custom kernel
222 - name: Create ARM VM with custom kernel
223 community.libvirt.virt_install:
224 name: arm-test-vm
225 memory: 1024
226 vcpus: 2
227 arch: armv7l
228 machine: vexpress-a9
229 disks:
230 - path: /home/user/VMs/myarmdisk.img
231 boot:
232 kernel: /tmp/my-arm-kernel
233 initrd: /tmp/my-arm-initrd
234 dtb: /tmp/my-arm-dtb
235 kernel_args: "console=ttyAMA0 rw root=/dev/mmcblk0p3"
236 graphics:
237 type: none
238 networks:
239 - network: default
240
241 # VM with SEV launch security (AMD)
242 - name: Create SEV-enabled VM
243 community.libvirt.virt_install:
244 name: sev-vm
245 memory: 4096
246 memtune:
247 hard_limit: 4563402
248 vcpus: 4
249 machine: q35
250 boot: uefi
251 disks:
252 - size: 15
253 bus: scsi
254 controller_devices:
255 - type: scsi
256 model: virtio-scsi
257 driver:
258 iommu: "on"
259 - type: virtio-serial
260 driver:
261 iommu: "on"
262 networks:
263 - network: default
264 model:
265 type: virtio
266 driver:
267 iommu: "on"
268 rng:
269 backend:
270 type: random
271 source: /dev/random
272 driver:
273 iommu: "on"
274 memballoon:
275 model: virtio
276 driver:
277 iommu: "on"
278 launch_security:
279 type: sev
280 policy: "0x01"
281 osinfo:
282 name: fedora39
283 import: true
284
285
286 # VM with PCI passthrough for a network device
287 - name: Create VM with PCI network passthrough
288 community.libvirt.virt_install:
289 name: passthrough-vm
290 memory: 4096
291 vcpus: 4
292 disks:
293 - size: 30
294 osinfo:
295 name: ubuntu20.04
296 import: true
297 host_devices:
298 - value: "81:00.0"
299
300 # Recreate existing VM
301 - name: Recreate existing VM with new configuration
302 community.libvirt.virt_install:
303 name: existing-vm
304 state: present
305 recreate: true
306 memory: 4096
307 vcpus: 4
308 disks:
309 - size: 40
310 osinfo:
311 name: fedora39
312 cdrom: /path/to/fedora39.iso
313 networks:
314 - network: default
315
316 # Remove VM
317 - name: Remove VM
318 community.libvirt.virt_install:
319 name: unwanted-vm
320 state: absent
321 """
322
323 RETURN = r"""
324 commands:
325 description:
326 - List of virt-install commands executed by this module.
327 - Only populated when a VM is created or recreated (V(state=present) and VM did not already exist, or V(recreate=true)).
328 - In check mode, the command includes the C(--dry-run) flag.
329 type: list
330 elements: str
331 returned: when a virt-install command was executed
332 version_added: "2.2.0"
333 sample: ["virt-install --name test-vm --memory 2048 --vcpus 2 --import --disk path=/var/lib/libvirt/images/test.qcow2"]
334 """
335
336 from ansible_collections.community.libvirt.plugins.module_utils import virt_install as virtinst_util
337 from ansible_collections.community.libvirt.plugins.module_utils.virt_install import (
338 LibvirtWrapper, VirtInstallTool
339 )
340 from ansible_collections.community.libvirt.plugins.module_utils.libvirt import (
341 HAS_VIRT, HAS_XML, VMNotFound, VIRT_SUCCESS
342 )
343 from ansible.module_utils.basic import AnsibleModule
344
345
346 def core(module):
347 state = module.params.get('state')
348 name = module.params.get('name')
349 uri = module.params.get('uri')
350 recreate = module.params.get('recreate', False)
351
352 result = dict(
353 changed=False,
354 original_message="", # TODO: Remove this unused return field in v3.0.0
355 message="",
356 commands=[],
357 )
358
359 virtConn = LibvirtWrapper(module)
360 virtInstall = VirtInstallTool(module)
361
362 if not name:
363 module.fail_json(msg="virtual machine name is missing")
364
365 vm_exists = False
366 try:
367 vm = virtConn.find_vm(name)
368 vm_exists = True
369 except VMNotFound:
370 vm_exists = False
371
372 if state == 'present':
373 if vm_exists and not recreate:
374 result['message'] = "virtual machine '%s' already exists" % name
375 return VIRT_SUCCESS, result
376 elif vm_exists and recreate:
377 if vm.isActive():
378 virtConn.destroy(name)
379 virtConn.undefine(name)
380
381 # run virt-install to create new vm
382 changed, rc, extra_res = virtInstall.execute(dryrun=module.check_mode)
383 result['changed'] = changed
384 result['commands'].extend(virtInstall.get_commands())
385 result.update(extra_res)
386
387 return rc, result
388 elif state == 'absent':
389 if not vm_exists:
390 result['message'] = "virtual machine '%s' is already absent" % name
391 return VIRT_SUCCESS, result
392
393 if vm.isActive():
394 virtConn.destroy(name)
395 virtConn.undefine(name)
396
397 result["changed"] = True
398 return VIRT_SUCCESS, result
399
400 module.fail_json(msg="unsupported state '%s'" % state)
401
402
403 def main():
404 """Main module entry point"""
405
406 # Define argument specification
407 argument_spec = dict(
408 # Connection options
409 uri=dict(type='str', default="qemu:///system"),
410 # Basic VM options
411 name=dict(type='str', required=True),
412 state=dict(
413 type='str',
414 choices=['present', 'absent'],
415 default='present'),
416 recreate=dict(type='bool', default=False),
417 )
418
419 # General options
420 argument_spec.update(virtinst_util.get_memory_args())
421 argument_spec.update(virtinst_util.get_memorybacking_args())
422 argument_spec.update(virtinst_util.get_arch_args())
423 argument_spec.update(virtinst_util.get_machine_args())
424 argument_spec.update(virtinst_util.get_metadata_args())
425 argument_spec.update(virtinst_util.get_events_args())
426 argument_spec.update(virtinst_util.get_resource_args())
427 argument_spec.update(virtinst_util.get_sysinfo_args())
428 argument_spec.update(virtinst_util.get_qmeu_commandline_args())
429 argument_spec.update(virtinst_util.get_vcpu_args())
430 argument_spec.update(virtinst_util.get_numatune_args())
431 argument_spec.update(virtinst_util.get_memtune_args())
432 argument_spec.update(virtinst_util.get_blkiotune_args())
433 argument_spec.update(virtinst_util.get_cpu_args())
434 argument_spec.update(virtinst_util.get_cputune_args())
435 argument_spec.update(virtinst_util.get_security_args())
436 argument_spec.update(virtinst_util.get_keywrap_args())
437 argument_spec.update(virtinst_util.get_iothreads_args())
438 argument_spec.update(virtinst_util.get_features_args())
439 argument_spec.update(virtinst_util.get_clock_args())
440 argument_spec.update(virtinst_util.get_pm_args())
441 argument_spec.update(virtinst_util.get_launch_security_args())
442 # Installation options
443 argument_spec.update(virtinst_util.get_cdrom_args())
444 argument_spec.update(virtinst_util.get_location_opts())
445 argument_spec.update(virtinst_util.get_pxe_args())
446 argument_spec.update(virtinst_util.get_import_args())
447 argument_spec.update(virtinst_util.get_extra_args_args())
448 argument_spec.update(virtinst_util.get_initrd_inject_args())
449 argument_spec.update(virtinst_util.get_install_args())
450 argument_spec.update(virtinst_util.get_unattended_args())
451 argument_spec.update(virtinst_util.get_cloud_init_args())
452 argument_spec.update(virtinst_util.get_boot_args())
453 argument_spec.update(virtinst_util.get_idmap_args())
454 # Guest OS options
455 argument_spec.update(virtinst_util.get_osinfo_args())
456 # Storage options
457 argument_spec.update(virtinst_util.get_disks_args())
458 argument_spec.update(virtinst_util.get_filesystems_args())
459 # Network options
460 argument_spec.update(virtinst_util.get_networks_args())
461 # Graphics options
462 argument_spec.update(virtinst_util.get_graphics_args())
463 # Virtualization options
464 argument_spec.update(virtinst_util.get_virt_type_args())
465 argument_spec.update(virtinst_util.get_hvm_args())
466 argument_spec.update(virtinst_util.get_paravirt_args())
467 argument_spec.update(virtinst_util.get_container_args())
468 # Device options
469 argument_spec.update(virtinst_util.get_controller_args())
470 argument_spec.update(virtinst_util.get_input_args())
471 argument_spec.update(virtinst_util.get_hostdev_args())
472 argument_spec.update(virtinst_util.get_sound_args())
473 argument_spec.update(virtinst_util.get_audio_args())
474 argument_spec.update(virtinst_util.get_watchdog_args())
475 argument_spec.update(virtinst_util.get_serial_args())
476 argument_spec.update(virtinst_util.get_parallel_args())
477 argument_spec.update(virtinst_util.get_channel_args())
478 argument_spec.update(virtinst_util.get_console_args())
479 argument_spec.update(virtinst_util.get_video_args())
480 argument_spec.update(virtinst_util.get_smartcard_args())
481 argument_spec.update(virtinst_util.get_redirdev_args())
482 argument_spec.update(virtinst_util.get_memballoon_args())
483 argument_spec.update(virtinst_util.get_tpm_args())
484 argument_spec.update(virtinst_util.get_rng_args())
485 argument_spec.update(virtinst_util.get_panic_args())
486 argument_spec.update(virtinst_util.get_shmem_args())
487 argument_spec.update(virtinst_util.get_vsock_args())
488 argument_spec.update(virtinst_util.get_iommu_args())
489 # Miscellaneous options
490 argument_spec.update(virtinst_util.get_autostart_args())
491 argument_spec.update(virtinst_util.get_transient_args())
492 argument_spec.update(virtinst_util.get_destroy_on_exit_args())
493 argument_spec.update(virtinst_util.get_noreboot_args())
494
495 # Create module
496 module = AnsibleModule(
497 argument_spec=argument_spec,
498 supports_check_mode=True)
499
500 if not HAS_VIRT:
501 module.fail_json(
502 msg="The `libvirt` module is not importable. Check the requirements.")
503
504 if not HAS_XML:
505 module.fail_json(
506 msg='The `lxml` module is not importable. Check the requirements.'
507 )
508
509 rc = VIRT_SUCCESS
510 try:
511 rc, result = core(module)
512 except Exception as e:
513 module.fail_json(msg=str(e))
514
515 if rc != 0: # something went wrong emit the msg
516 module.fail_json(rc=rc, msg=result)
517 else:
518 module.exit_json(**result)
519
520
521 if __name__ == "__main__":
522 main()