Skip to content

Commit d7bb5b4

Browse files
author
Will Miller
committed
Don't reuse variable names in playbooks
Also, ensure rejectattr condition works properly, and use boolean mode for default filters so defaults work in the case of using deprecated variables.
1 parent e5cf3c3 commit d7bb5b4

File tree

6 files changed

+32
-35
lines changed

6 files changed

+32
-35
lines changed

tasks/destroy-vm.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@
33
# exists before destroying it.
44
- name: Check the VM's status
55
virt:
6-
name: "{{ libvirt_vm_name }}"
6+
name: "{{ vm.name }}"
77
command: list_vms
88
register: result
99
become: yes
1010

1111
- block:
1212
- name: Ensure the VM is absent
1313
virt:
14-
name: "{{ libvirt_vm_name }}"
14+
name: "{{ vm.name }}"
1515
state: destroyed
1616
become: yes
1717

1818
- name: Ensure the VM is undefined
1919
virt:
20-
name: "{{ libvirt_vm_name }}"
20+
name: "{{ vm.name }}"
2121
command: undefine
2222
become: yes
23-
when: libvirt_vm_name in result.list_vms
23+
when: vm.name in result.list_vms

tasks/destroy-volumes.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
destroy_virt_volume.sh
55
{{ item.name }}
66
{{ item.pool }}
7-
with_items: "{{ libvirt_vm_volumes }}"
7+
with_items: "{{ volumes }}"
88
register: volume_result
99
changed_when:
1010
- volume_result is success

tasks/main.yml

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
---
22
- include_tasks: autodetect.yml
33
# We need to know the engine and emulator if we're creating any new VMs.
4-
when: "libvirt_vms | rejectattr('state', 'eq', 'absent')"
4+
when: libvirt_vms | rejectattr('state', 'eq', 'absent') | list
55

66
- include_tasks: volumes.yml
77
vars:
8-
libvirt_vm_volumes: "{{ vm.volumes | default([]) }}"
8+
volumes: "{{ vm.volumes | default([], true) }}"
99
with_items: "{{ libvirt_vms }}"
1010
loop_control:
1111
loop_var: vm
12-
when: (vm.state | default('present')) == 'present'
12+
when: (vm.state | default('present', true)) == 'present'
1313

1414
- include_tasks: vm.yml
1515
vars:
16-
libvirt_vm_console_log_path: "{{ vm.console_log_path | default(libvirt_vm_default_console_log_dir + '/' + vm.name + 'console.log') }}"
17-
libvirt_vm_name: "{{ vm.name }}"
16+
console_log_path: "{{ vm.console_log_path | default(libvirt_vm_default_console_log_dir + '/' + vm.name + 'console.log', true) }}"
1817
machine_default: "{{ none if libvirt_vm_engine == 'kvm' else 'pc-1.0' }}"
19-
libvirt_vm_machine: "{{ vm.machine | default(machine_default) }}"
18+
machine: "{{ vm.machine | default(machine_default, true) }}"
2019
cpu_mode_default: "{{ 'host-passthrough' if libvirt_vm_engine == 'kvm' else 'host-model' }}"
21-
libvirt_vm_cpu_mode: "{{ vm.cpu_mode | default(cpu_mode_default) }}"
22-
libvirt_vm_volumes: "{{ vm.volumes | default([]) }}"
23-
libvirt_vm_interfaces: "{{ vm.interfaces | default([]) }}"
20+
cpu_mode: "{{ vm.cpu_mode | default(cpu_mode_default, true) }}"
21+
volumes: "{{ vm.volumes | default([], true) }}"
22+
interfaces: "{{ vm.interfaces | default([], true) }}"
2423
with_items: "{{ libvirt_vms }}"
2524
loop_control:
2625
loop_var: vm
27-
when: (vm.state | default('present')) == 'present'
26+
when: (vm.state | default('present', true)) == 'present'
2827

2928
- include_tasks: destroy-volumes.yml
3029
vars:
31-
libvirt_vm_volumes: "{{ vm.volumes | default([]) }}"
30+
volumes: "{{ vm.volumes | default([], true) }}"
3231
with_items: "{{ libvirt_vms }}"
3332
loop_control:
3433
loop_var: vm
35-
when: (vm.state | default('present')) == 'absent'
34+
when: (vm.state | default('present', true)) == 'absent'
3635

3736
- include_tasks: destroy-vm.yml
38-
vars:
39-
libvirt_vm_name: "{{ vm.name }}"
4037
with_items: "{{ libvirt_vms }}"
4138
loop_control:
4239
loop_var: vm
43-
when: (vm.state | default('present')) == 'absent'
40+
when: (vm.state | default('present', true)) == 'absent'

tasks/vm.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
- name: Ensure the VM console log directory exists
33
file:
4-
path: "{{ libvirt_vm_console_log_path | dirname }}"
4+
path: "{{ console_log_path | dirname }}"
55
state: directory
66
owner: qemu
77
group: qemu
@@ -11,14 +11,14 @@
1111

1212
- name: Ensure the VM is defined
1313
virt:
14-
name: "{{ libvirt_vm_name }}"
14+
name: "{{ vm.name }}"
1515
command: define
1616
xml: "{{ lookup('template', 'vm.xml.j2') }}"
1717
become: true
1818

1919
- name: Ensure the VM is running and started at boot
2020
virt:
21-
name: "{{ libvirt_vm_name }}"
21+
name: "{{ vm.name }}"
2222
autostart: true
2323
state: running
2424
become: true

tasks/volumes.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
get_url:
44
url: "{{ item }}"
55
dest: "{{ libvirt_vm_image_cache_path }}/{{ item | basename }}"
6-
with_items: "{{ libvirt_vm_volumes | selectattr('image', 'defined') | map(attribute='image') | list }}"
6+
with_items: "{{ volumes | selectattr('image', 'defined') | map(attribute='image') | list }}"
77
when: "'http' in item"
88

99
- name: Ensure local images are copied
1010
copy:
1111
src: "{{ item }}"
1212
dest: "{{ libvirt_vm_image_cache_path }}/{{ item | basename }}"
13-
with_items: "{{ libvirt_vm_volumes | selectattr('image', 'defined') | map(attribute='image') | list }}"
13+
with_items: "{{ volumes | selectattr('image', 'defined') | map(attribute='image') | list }}"
1414
when: "'http' not in item"
1515

1616
- name: Ensure the VM volumes exist
@@ -23,7 +23,7 @@
2323
{% if item.image is defined %}
2424
{{ libvirt_vm_image_cache_path }}/{{ item.image | basename }}
2525
{% endif %}
26-
with_items: "{{ libvirt_vm_volumes }}"
26+
with_items: "{{ volumes }}"
2727
register: volume_result
2828
changed_when:
2929
- volume_result is success

templates/vm.xml.j2

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
<domain type='{{ libvirt_vm_engine }}'>
2-
<name>{{ libvirt_vm_name }}</name>
3-
<memory>{{ libvirt_vm_memory_mb | int * 1024 }}</memory>
4-
<vcpu>{{ libvirt_vm_vcpus }}</vcpu>
2+
<name>{{ vm.name }}</name>
3+
<memory>{{ vm.memory_mb | int * 1024 }}</memory>
4+
<vcpu>{{ vm.vcpus }}</vcpu>
55
<clock sync="localtime"/>
66
<on_poweroff>destroy</on_poweroff>
77
<on_reboot>restart</on_reboot>
88
<on_crash>destroy</on_crash>
99
<os>
10-
<type arch='{{ libvirt_vm_arch }}'{% if libvirt_vm_machine is not none %} machine='{{ libvirt_vm_machine }}'{% endif %}>hvm</type>
10+
<type arch='{{ libvirt_vm_arch }}'{% if machine is not none %} machine='{{ machine }}'{% endif %}>hvm</type>
1111
<bootmenu enable='no'/>
1212
<boot dev='hd'/>
1313
<boot dev='network'/>
1414
<bios useserial='yes'/>
1515
</os>
16-
<cpu{% if libvirt_vm_cpu_mode is not none %} mode='{{ libvirt_vm_cpu_mode }}'{% endif %}>
16+
<cpu{% if cpu_mode is not none %} mode='{{ cpu_mode }}'{% endif %}>
1717
<model fallback='allow'/>
1818
</cpu>
1919
<devices>
2020
<emulator>{{ libvirt_vm_emulator }}</emulator>
21-
{% for volume in libvirt_vm_volumes %}
21+
{% for volume in volumes %}
2222
<disk type='volume' device='{{ volume.device | default('disk') }}'>
2323
<driver name='qemu' type='{{ volume.format }}'/>
2424
<source pool='{{ volume.pool }}' volume='{{ volume.name }}'/>
2525
<target dev='vd{{ 'abcdefghijklmnopqrstuvwxyz'[loop.index] }}'/>
2626
</disk>
2727
{% endfor %}
28-
{% for interface in libvirt_vm_interfaces %}
28+
{% for interface in interfaces %}
2929
<interface type='network'>
3030
<source network='{{ interface.network }}'/>
3131
<model type='virtio'/>
3232
</interface>
3333
{% endfor %}
3434
<serial type='file'>
35-
<source path='{{ libvirt_vm_console_log_path }}'/>
35+
<source path='{{ console_log_path }}'/>
3636
</serial>
3737
<serial type='pty'/>
3838
<console type='file'>
39-
<source path='{{ libvirt_vm_console_log_path }}'/>
39+
<source path='{{ console_log_path }}'/>
4040
<target type='serial'/>
4141
</console>
4242
</devices>

0 commit comments

Comments
 (0)