Ansible Create KVM Guests
-
The Ansible virt module doesn't have a clone option and the creation of guests is a little limited. Because of this we have to use the shell or command modules and try to make them idempotent. This is a simple example and the dictionary can be expanded to a lot more customization. There is a way to use libvirt as a dynamic inventory and set group and host vars on guests, but I'm not going into that here.
--- - name: create VMs hosts: kvm become: true vars_files: - vms.yml tasks: - name: get VM disks command: "ls {{ vm_location }}" register: disks changed_when: "disks.rc != 0" - name: create disk command: > virt-builder --format qcow2 centos-7.4 -o {{ vm_location}}/{{ item.key }}.{{ item.value.file_type }} --root-password password:{{ root_pass }} when: item.key not in disks.stdout with_dict: "{{ guests }}" - name: get list of VMs virt: command: "list_vms" register: vms - name: create vm command: > virt-install --import --name {{ item.key }} --memory {{ item.value.mem }} --vcpus {{ item.value.cpus }} --disk {{ vm_location }}/{{ item.key }}.{{ item.value.file_type }} --noautoconsole --os-variant {{ item.value.os_type }} when: item.key not in vms.list_vms with_dict: "{{ guests }}" - name: start vm virt: name: "{{ item.key }}" state: running with_dict: "{{ guests }}"
So we do a few checks to make sure the disk isn't already in the directory and another check to make sure the VM isn't already created. The dictionary is in the referenced vars_file:
--- vm_location: "/data/VMs" root_pass: "password" guests: test: mem: 512 cpus: 1 os_type: rhel7 file_type: qcow2 test2: mem: 512 cpus: 1 os_type: rhel7 file_type: qcow2
Obviously you wouldn't put the password in plain text, you'd either use ansible-vault, a vars prompt, or a survey in tower.
And here's our output. I had already created a system called test so it skips over to give us idempotence.
-
I might be missing some basic step. But in order to make it work what else is needed? on ly running this returns
'PLAY [create VMs] **************************************************************
skipping: no hosts matched' -
@humax159 said in Ansible Create KVM Guests:
I might be missing some basic step. But in order to make it work what else is needed? on ly running this returns
'PLAY [create VMs] **************************************************************
skipping: no hosts matched'Did you create and populate the hosts file?
-
@humax159 namely did you create the "kvm" label for your virtualization server?!
-
@matteo-nunziati said in Ansible Create KVM Guests:
@humax159 namely did you create the "kvm" label for your virtualization server?!
Yeah if you copy this line for line there has to be a host or group called KVM.
If you only have a single host your inventory needs to have the hostname/ip.
[hosts] kvm.example.com
Or this
[hosts] 10.0.0.5 ansible_host=kvm
-
Good information
-
After I wrote this I found a module that someone wrote (Ansible lets you create custom modules) and it includes the clone feature. When I get time I'll try to write that up.
-
Hi @stacksofplates. Did you find something about virt module.
Here's https://docs.ansible.com/ansible/latest/modules/virt_module.html#virt-module link.But, I cannot build a machine? with creating command.
by the way: your article is awesome. -
@fabionoth said in Ansible Create KVM Guests:
Hi @stacksofplates. Did you find something about virt module.
Here's https://docs.ansible.com/ansible/latest/modules/virt_module.html#virt-module link.But, I cannot build a machine? with creating command.
by the way: your article is awesome.Yeah that's why I wrote this. That virt module doesn't support cloning systems. I never got around to linking that module. I'll see if I can find it again. You just need to put it in your local module library and then you can call it like any other module.