- name: Copy .service files from files folder to /etc/systemd/system
  find:
    paths: "files/"
    hidden: yes
    file_type: file
    patterns: "*.service"
  register: service_files

- name: Copy each .service file to systemd directory
  copy:
    src: "{{ item.path }}"
    dest: "/etc/systemd/system" 
    owner: root
    group: root
    mode: '0644'
  with_items: "{{ service_files.files }}"
  when: service_files is defined   

- name: Combine predefined list and filesystem paths
  set_fact:
    systemd_service: "{{ systemd_service | union(service_files.files | map(attribute='path') | map('basename')) | unique }}"
  when: service_files is defined   

- name: Enable Systemd Services
  ansible.builtin.systemd_service:
    name: "{{ item }}"
    state: started
    enabled: true
  with_items: "{{ systemd_service }}"
  when: systemd_service is defined   
