ansible-collections / community.libvirt

Manage libvirt with Ansible
http://galaxy.ansible.com/community/libvirt
GNU General Public License v3.0
61 stars 40 forks source link

ansible-playbook --check mode/diff support for virt module #98

Open nodiscc opened 2 years ago

nodiscc commented 2 years ago
SUMMARY

The virt module does not seem to support ansible-playbook --check mode - in check mode, tasks that use the virt module will always return skipped.

ISSUE TYPE
COMPONENT NAME

virt

ADDITIONAL INFORMATION

This would allow reviewing changes in "dry-run" mode before actually applying them. Running the virt module in check mode should return the expected ok/changed state, and a diff of the VM XML definition when relevant (for example when command: define).

ansible-playbook playbook.yml --tags libvirt --check --diff
csmart commented 2 years ago

Hi @nodiscc, thanks for the suggestion! I'll need to think about this some more and have some conversations with others more knowledgeable than I am. I think that check returning OK is normal for tasks, so we could probably improve the check code there. I can see the benefit of the diff mode, but I'm not sure about how to implement it exactly. I think we should also take a look at what other modules do, for inspiration and consistency. (For future reference, https://docs.ansible.com/ansible/latest/user_guide/playbooks_checkmode.html#using-diff-mode)

What sort of things would you expect to see in the diff? For example, if you had a task to power a VM off which was currently on, or to add another device to an existing VM, etc?

Cheers, -c

nodiscc commented 2 years ago

Hi @csmart , sorry for the late reply

A very basic use case for --check mode would be simulating VM startup/shutdown, for example I have a task that starts 3 VMs:

- name: start libvirt VMs
  community.libvirt.virt:
    name: "{{ item.name }}"
    state: "{{ item.state }}"
  with_items:
    - name: vm1.example.org
      state: running
    - name: vm2.example.org
      state: running
    - name: vm3.example.org
      state: running

When executed in check mode, the task returns skipping for all items:

# ansible-playbook --check playbook.yml --tags libvirt --limit hv.example.org
TASK [hv.example.org : start libvirt VMs] **************************************************************
skipping: [hv.example.org] => (item={'name': 'vm1.example.org', 'state': 'running'}) 
skipping: [hv.example.org] => (item={'name': 'vm2.example.org', 'state': 'running'}) 
skipping: [hv.example.org] => (item={'name': 'vm3.example.org', 'state': 'running'}) 

But I would expect it to return the same output as without --check:

# ansible-playbook playbook.yml --tags libvirt --limit hv.example.org
TASK [hv.example.org : start libvirt VMs] **************************************************************
ok: [hv.example.org] => (item={'name': 'vm1.example.org', 'state': 'running'})
changed: [hv.example.org] => (item={'name': 'vm2.example.org', 'state': 'running'})
changed: [hv.example.org] => (item={'name': 'vm3.example.org', 'state': 'running'})

(vm1 was already on, vm2 and vm3 were just started)

Thanks