---
# Creates a standalone KVM hosts
# Created: @SherifNagy
# Modified to current standards: @nazunalika
- name: Configure KVM host
  hosts: kvm
  become: true

  pre_tasks:
    - name: Check if ansible cannot be run here
      stat:
        path: /etc/no-ansible
      register: no_ansible

    - name: Verify if we can run ansible
      ansible.builtin.assert:
        that:
          - "not no_ansible.stat.exists"
        success_msg: "We are able to run on this node"
        fail_msg: "/etc/no-ansible exists - skipping run on this node"

  tasks:
    - name: Check for CPU Virtualization
      shell: "set -o pipefail; lscpu | grep -i virtualization"
      register: result
      changed_when: false
      failed_when: "result.rc != 0"

    # Install KVM packages
    - name: Installing KVM Packages
      ansible.builtin.package:
        name:
          - qemu-kvm
          - libvirt
          - libvirt-python
          - libguestfs-tools
          - virt-install
        state: present

    - name: Enable and Start libvirtd
      ansible.builtin.systemd:
        name: libvirtd
        state: started
        enabled: true

    - name: Verify KVM module is loaded
      shell: "set -o pipefail; lsmod | grep -i kvm"
      register: result
      changed_when: false
      failed_when: "result.rc != 0"

  post_tasks:
    - name: Touching run file that ansible has ran here
      ansible.builtin.file:
        path: /var/log/ansible.run
        state: touch
        mode: '0644'
        owner: root
        group: root
...