-
Notifications
You must be signed in to change notification settings - Fork 73
Allow specification of multiple VMs #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, looks like a good start.
tasks/destroy-volumes.yml
Outdated
@@ -4,8 +4,9 @@ | |||
destroy_virt_volume.sh | |||
{{ item.name }} | |||
{{ item.pool }} | |||
with_items: "{{ libvirt_vm_volumes }}" | |||
with_items: "{{ vm.volumes | default([]) }}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that this default will work in the same way as before. The default filter only applies the default if the variable is undefined. I think there are two potential solutions. Either omit the deprecated variables from defaults/main.yml, such that they are actually undefined, or use the boolean argument to default. Possibly the first is more consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same goes for other defaults.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. I don't think omitting the deprecated variables from defaults/main.yml
will work, because they are being referenced in the default singleton definition of libvirt_vms
, and we get an error of referencing an undefined variable. Even if we use default(omit) filters in each element of the libvirt_vms
definition, omitting role variables doesn't work.
I guess using default's boolean argument is the best option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having said all that, using your vars suggestion means we don't need the default filters at all!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did a little testing and looks like you're right. Don't we need to use default when defining the vars though?
tasks/vm.yml
Outdated
@@ -1,7 +1,7 @@ | |||
--- | |||
- name: Ensure the VM console log directory exists | |||
file: | |||
path: "{{ libvirt_vm_console_log_path | dirname }}" | |||
path: "{{ vm.console_log_path | default('/var/log/libvirt-consoles/' + vm.name + '.log') | dirname }}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might improve usability to make the default directory configurable.
templates/vm.xml.j2
Outdated
<os> | ||
<type arch='{{ libvirt_vm_arch }}'{% if libvirt_vm_machine is not none %} machine='{{ libvirt_vm_machine }}'{% endif %}>hvm</type> | ||
<type arch='{{ libvirt_vm_arch }}'{% if vm.machine is defined %} machine='{{ vm.machine | default('pc-1.0') }}'{% endif %}>hvm</type> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure the logic is quite right here. Docs say:
Default is None
if engine
is kvm
, otherwise pc-1.0
.
You can use {% set var=val %} if the logic gets hairy.
templates/vm.xml.j2
Outdated
<bootmenu enable='no'/> | ||
<boot dev='hd'/> | ||
<boot dev='network'/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this will continue to work for non network-booted VMs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like when I boot a VM with a hard drive and no network interface, it successfully boots from the hard drive even with this configuration.
templates/vm.xml.j2
Outdated
<bios useserial='yes'/> | ||
</os> | ||
<cpu{% if libvirt_vm_cpu_mode is not none %} mode='{{ libvirt_vm_cpu_mode }}'{% endif %}> | ||
<cpu mode='{% if vm.cpu_mode is defined and vm.cpu_mode is not none %}{{ vm.cpu_mode }}{% elif libvirt_vm_engine == 'kvm' %}host_passthrough{% else %}host-model{% endif %}'> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
host-passthrough
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good spot.
templates/vm.xml.j2
Outdated
</serial> | ||
<serial type='pty'/> | ||
<console type='file'> | ||
<source path='{{ libvirt_vm_console_log_path }}'/> | ||
<source path='{{ vm.console_log_path | default('/var/log/libvirt-consoles/' + vm.name + '.log') }}'/> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you wanted to avoid repeating this logic, you could define a per-task variable. You could even do something like this in main.yml:
- include_tasks: create_vm.yml
vars:
vm_console_path: "{{ vm.console_log_path | default('/var/log/libvirt-consoles/' + vm.name + '.log') }}"
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't believe I didn't think of that - much better idea.
Adapt the role to allow a list of VM specifications. This involves: * Splitting libvirt_vm_* variables into role-scope and VM-scope. * Deprecating the old set of variables.
Libvirt pools are created with `become` in ansible-role-libvirt-host, so the volumes created here must also use `become` to ensure they can access the created pools.
Also, set power toggles for virsh.
Updated and ready for re-review. |
tasks/main.yml
Outdated
with_items: "{{ libvirt_vms }}" | ||
loop_control: | ||
loop_var: vm | ||
when: (vm.state | default('present')) == 'present' | ||
|
||
- include_tasks: vm.yml | ||
vars: | ||
libvirt_vm_console_log_path: "{{ vm.console_log_path | default(libvirt_vm_default_console_log_dir + '/' + vm.name + 'console.log') }}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need a hyphen before console.log?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep.
tasks/main.yml
Outdated
cpu_mode_default: "{{ 'host-passthrough' if libvirt_vm_engine == 'kvm' else 'host-model' }}" | ||
libvirt_vm_cpu_mode: "{{ vm.cpu_mode | default(cpu_mode_default) }}" | ||
libvirt_vm_volumes: "{{ vm.volumes | default([]) }}" | ||
libvirt_vm_interfaces: "{{ vm.interfaces | default([]) }}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there any negative consequences of reusing these variable names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't think there would be, but I shouldn't have been so naïve. Testing revealed some strange errors in variable scope when redefining these so I'll give them different names.
# See README.md or tasks/main.yml for these attributes' defaults. | ||
libvirt_vms: | ||
# State of the VM. May be 'present' or 'absent'. | ||
- state: "{{ libvirt_vm_state }}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't think the defaults will work correctly in this case, since the variable is defined - it will be None. How about just keeping the old defaults on the deprecated variables?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, true. It seems more consistent to use the boolean flag in the default filter for all the variables though, rather than putting some of the defaults in here.
Also, ensure rejectattr condition works properly, and use boolean mode for default filters so defaults work in the case of using deprecated variables.
Updated and ready for re-review. I've tried to test a few scenarios a bit more thoroughly this time, with regard to defaults. |
/var/log/libvirt/ has permissions issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks sensible to me.
Does this need some more reviewers? |
3e886a7
to
da56e8c
Compare
Logging the console to a file prevents a serial PTY from also being available, which is not ideal. Add a toggle to enable console logging which defaults to false, meaning the old PTY behaviour is maintained by default.
Console log file enabled toggle
Since I'd based this branch off |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff Will.
Also some other minor fixes.