Skip to content

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

Merged
merged 15 commits into from
Aug 3, 2018

Conversation

w-miller
Copy link
Contributor

Also some other minor fixes.

Copy link

@markgoddard markgoddard left a 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.

@@ -4,8 +4,9 @@
destroy_virt_volume.sh
{{ item.name }}
{{ item.pool }}
with_items: "{{ libvirt_vm_volumes }}"
with_items: "{{ vm.volumes | default([]) }}"

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.

See http://jinja.pocoo.org/docs/2.10/templates/#default.

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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!

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 }}"

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.

<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>

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.

<bootmenu enable='no'/>
<boot dev='hd'/>
<boot dev='network'/>

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?

Copy link
Contributor Author

@w-miller w-miller Jul 17, 2018

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.

<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 %}'>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

host-passthrough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot.

</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') }}'/>

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') }}"
    ...

Copy link
Contributor Author

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.

Will Miller added 6 commits July 17, 2018 15:12
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.
@w-miller
Copy link
Contributor Author

Updated and ready for re-review.

@w-miller w-miller closed this Jul 17, 2018
@w-miller w-miller reopened this Jul 17, 2018
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') }}"

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?

Copy link
Contributor Author

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([]) }}"

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?

Copy link
Contributor Author

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 }}"

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?

Copy link
Contributor Author

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.

Will Miller added 3 commits July 18, 2018 10:30
Also, ensure rejectattr condition works properly, and use boolean mode
for default filters so defaults work in the case of using deprecated
variables.
@w-miller
Copy link
Contributor Author

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.
Copy link

@markgoddard markgoddard left a 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.

@w-miller
Copy link
Contributor Author

w-miller commented Aug 2, 2018

Does this need some more reviewers?

@markgoddard markgoddard force-pushed the support-virt-engine branch from 3e886a7 to da56e8c Compare August 2, 2018 15:41
@markgoddard markgoddard changed the base branch from support-virt-engine to master August 2, 2018 15:43
Will Miller added 2 commits August 3, 2018 11:01
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
@w-miller
Copy link
Contributor Author

w-miller commented Aug 3, 2018

Since I'd based this branch off support-virt-engine which had console logging enabled already, I've merged the console logging toggle commits into this pull request.

Copy link

@markgoddard markgoddard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff Will.

@markgoddard markgoddard merged commit baba356 into stackhpc:master Aug 3, 2018
@w-miller w-miller deleted the vm_list2 branch August 16, 2018 10:39
markgoddard pushed a commit that referenced this pull request Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants