---
- name: "Ensure {{ app_name }} is installed"
  community.general.pacman:
    name:
      - cockpit
      - cockpit-machines
      - libvirt-dbus
    state: present
    update_cache: "{{ omit if skip_package_cache_update is defined else 'true' }}"

- name: Enable the Cockpit service
  systemd:
    enabled: true
    name: cockpit.socket
    state: started

- name: Gather the package facts
  package_facts:
    manager: auto

- name: Install Cockpit Podman
  community.general.pacman:
    name: cockpit-podman
    state: present
  when: (ansible_facts.packages | regex_search('docker*')) or (ansible_facts.packages | regex_search('podman*'))

- name: Add Cockpit machine profiles
  template:
    src: cockpit-machine.json.j2
    dest: "{{ cockpit_machines_path }}/{{ item.name }}.json"
    owner: root
    mode: 0644
  loop: "{{ cockpit_instances }}"
  when: cockpit_instances is defined

- name: Add libvirt user to libvirtdbus group
  user:
    append: true
    comment: Required by Cockpit
    create_home: false
    groups:
      - libvirtdbus
    name: libvirt
    state: present
    system: true
  notify: reboot system
