Comments (9)
@csmart can @Tronde 's work be somehow incorporated with the project?
from community.libvirt.
Hi @x09 thank you for putting in this suggestion, much appreciated! I can see how a clone function could be useful. I expect that it would have to update MAC addresses and create new images, etc. Perhaps have to see how existing libvirt manages this, for example, selection of storage location, etc.
If you need this kind of functionality right now, I wonder if you could dump the XML from the virtual machine, modify it and define a new machine? Definitely a few more steps, but might be something to consider if it's useful for you. Another option might be to use a virsh
command with the Ansible shell
module.
Thanks again for the suggestion, we can continue to discuss any thoughts on this issue.
from community.libvirt.
Since it is not currently possible to properly clone a VM directly from ansible (or even from a single libvirt command), I have implemented a simple bash command-line wrapper to perform this operation and work around the quirks. You can find it here (L361-492). The quirks are:
- you need to edit the network configuration inside the clone if using static IP addressing (else IP addresses of the clone and the template would collide - I only implemented this for Debian/
/etc/network/intefaces
-based configuration) - run
virt-clone
(which also sets random UUID and MAC addresses) - run
virt-sysprep
with the correct options (this is important - else the resulting machines will all have the same SSH server key, machine ID, unique identifiers...), which also injects SSH keys, regenerates the server key, prepares user accounts, sets passwords...
You can try it like this:
$ wget https://raw.githubusercontent.com/nodiscc/xsrv/master/xsrv
$ chmod a+x xsrv
$ ./xsrv init-vm --help
USAGE: ./xsrv init-vm --name VM_NAME [--template debian11-base] --ip VM_IP --netmask VM_NETMASK --gateway VM_GATEWAY [--ssh-port VM_SSH_PORT] [--sudo-user deploy] [--sudo-password VM_SUDO_PASSWORD] --ssh-pubkey 'ssh-rsa AAAAB...' [--root-password VM_ROOT_PASSWORD] [--disk-path /path/to/my.CHANGEME.org.qcow2] [--memory 1024] [--vcpus NUM_CPU]
EXAMPLE: ./xsrv init-vm --template debian11-base --name my.CHANGEME.org --ip 10.0.0.223 --netmask 24 --gateway 10.0.0.1 --sudo-user deploy --sudo-password CHANGEME --ssh-pubkey 'ssh-rsa AAAAB...' --root-password CHANGEME --memory 3G --vcpus 4 [--dump]
Initialize a libvirt VM from a template, configure resources/users/SSH access, and start the VM.
Requirements: openssh-client sshpass libvirt virtinst libvirt-daemon-system libguestfs-tools pwgen netcat-openbsd util-linux
--template name of the template to create the new VM from (default debian11-base)
--name REQUIRED name of the VM to create
--ip REQUIRED IP address of the VM
--gateway REQUIRED default network gateway of the VM
--netmask network mask of the VM (CIDR notation, default 24)
--root-password root account password (default generate and display a random password)
--sudo-user administrative (sudoer) user account (default deploy)
--sudo-password password for the administrative (sudoer) user account (default generate and display a random password)
--ssh-pubkey REQUIRED public key to authorize on the administrative (sudoer) account
--disk-path path to the qcow2 disk image to create (default: /var/lib/libvirt/images/VM_NAME.qcow2)
--memory VM memory with M or G suffix (default 1G)
--vcpus number of vCPUs (default: same value as the template)
--dump display the VM XML definition after creation, for use with the nodiscc.xsrv.libvirt role
I would also be interested in having this functionality directly available from the libvirt collection/module. I imagine this would take the form of a clone_from: my_template_vm_name
parameter, and if the VM does not exist, it would be created from the template passed in this parameter. But as you can see there are many more parameters that one would want to change when cloning a VM, I don't know if it would be possible to manage them all though the module.
from community.libvirt.
I also have a mechanism for cloning VMs in my own fork of this repo (I've never found the time to write testcases for it, hence why no PR yet) - https://github.com/dseeley/community.libvirt/tree/develop:
plugins/modules/virt_volume.py
is a new module to create/clone a volume, as well as create a cloudinit cdrom (so you can e.g. set the networking parameters on a cloned vm).plugins/modules/virt.py
is modified to add the functions:get_guest_agent_info
,attach_device
,detach_device
,update_device
,set_metadata
(so that, amongst other reasons, the cloudinit cdrom can be attached and detached)
I use it in my cluster management software (https://github.com/dseeley/clusterverse), the relevant playbook of which is:
from community.libvirt.
Hello @x09, Hello All,
I have the same use case as described by @x09 in his initial post. I install some VMs to use them as templates to create new VMs for lab environments as I need them.
To do that I've developed an Ansible Role that enables you to specify the VMs you need and the template to use as a YAML dictionary. Then the role uses this dictionary to perform the following tasks:
- Get a list of VMs to check whether a VM with the same name already exists.
- Copy the base disk image.
- Configure the image (set root password, add ssh key, uninstall cloud-init, and selinux-relabel (only if selinux is installed in template)).
- Define VMs as specified in dictionary.
- Create a second disk if specified in dictionary and attach it.
- Ensure the VMs are started.
You find this role at:
- GitHub: https://github.com/Tronde/kvm_provision_lab
- Galaxy: https://galaxy.ansible.com/Tronde/kvm_provision_lab
Feel free to give it a try. I use it for some time now and would be happy if other folks find it useful, too. Any feedback is appreciated.
Best regards,
Tronde
from community.libvirt.
Thanks for the suggestion @Tronde and apologies for the delayed response. I think it would be good to include some example playbooks on how to define a machine, one that uses XML template and one that uses virt-install
, to help get users started. Although overall, I'd also prefer see the role improve from the Python side to add and improve features, if possible.
I say example playbooks because I'm not sure how possible it would be to include a universal playbook at this stage because it needs to be both flexible and robust, which I don't think is a particularly trivial undertaking (that's is why in my own role I've wrapped virt-install
as it includes more of the logic I can take advantage of). For example, supporting multiple disks, various disk types (SCSI/SATA/IDE/NVMe), SSD/rotational disks, disk image formats, multiple disk controllers and settings (SCSI/virtio/SATA, etc), multiple network cards on different networks, unique/defined mac addresses, CPU model, balloon settings, graphics cards, remote settings, serial, passthrough, etc, then tying all the required PCI devices and addresses together to make it work every time. We could potentially do that with some extensive logic in the XML file, but I'm just not sure how easy or worthwhile it would be - this is the kind of thing I'd love to see extended in the Python code.
Please feel free to create a merge request to add a generic playbook based off your work and I'll review and test it! I think it would be good to use as cut down an XML as possible and let libvirt
fill in the details when it brings up the VM and ideally if there are some tests so that we can automate testing it on multiple distros, that'd be great!
Please let me know how you go and reach out if you need a hand. Cheers!
from community.libvirt.
No worries @csmart and thanks a lot for your feedback. The role may improve over time as I use it in my lab environments. I'll show it again when it has advanced.
Cheers!
from community.libvirt.
Hey @Tronde, what I meant to say was that I think your role as it is would be great to add as an example playbook on how to use the collection to define a VM using an XML template. I didn't mean to suggest you need to go and make it more awesome before we can merge it in, so if you're happy to create a MR to get it in as an example for now, feel free. We can always work to improve it over time, but I'll leave it up to you. Cheers!
from community.libvirt.
Well, I guess I misunderstood you then. Let me give the role one more tweak and I'll send a merge regquest. :-)
from community.libvirt.
Related Issues (20)
- Is it really necessary to include both COPYING and LICENSE? HOT 1
- Add the ability to get the interface addresses of a Domain (VM)
- The libvirt module is not importable. Check the requirements HOT 5
- creating networks in "open" mode no longer works HOT 1
- 'list index out of range' when a glusterfs pool exists HOT 1
- Command not found when executing in VM HOT 2
- All modules and doc fragments: Change yes/no/True/False to true/false
- Collection CI related actions needed
- Split `virt` into specialized modules
- Consider using true/false for all booleans in docs HOT 1
- Squelch output related to "domain is not running" HOT 1
- encoding error on Windows guests with libvirt_qemu connection plugin HOT 2
- I can’t do “undefine” for a virtual machine with uefi nvram HOT 2
- Problem with file paths when using ansible.builtin.copy module HOT 5
- Missing lxml dependency declaration
- Unable to use virt module against pmsuspended guest
- Option to remove all storage when deleting VM's
- Templates already available in the collection HOT 2
- get_xml missing from return values in documentation for community.libvirt.virt
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from community.libvirt.