---
# tasks file for service
- name: test veriables and environment
  assert:
    that:
      - service_list is defined

- name: test service_list
  assert:
    that:
      - item.name is defined
      - item.description is defined
      - item.start_command is defined
  with_items:
    - "{{ service_list }}"
  loop_control:
    label: "{{ item.name }}"

- name: install required packages
  package:
    name: "{{ service_required_packages }}"
    state: present
  register: service_install_required_packages
  until: service_install_required_packages is succeeded
  retries: 3

- name: configure systemd service
  template:
    src: "systemd.j2"
    dest: "/etc/systemd/system/{{ item.name }}.service"
  when:
    - ansible_service_mgr == "systemd"
  notify:
    - systemctl daemon-reload
  with_items:
    - "{{ service_list }}"
  loop_control:
    label: "{{ item.name }}"

- name: configure sysvinit service
  template:
    src: "sysvinit.j2"
    dest: "/etc/init.d/{{ item.name }}"
    mode: "0755"
  when:
    - ansible_service_mgr == "sysvinit" or
      ansible_service_mgr == "upstart" or
      ansible_service_mgr == "openrc"
  with_items:
    - "{{ service_list }}"
  notify:
    - restart service
  loop_control:
    label: "{{ item.name }}"

- name: flush handlers
  meta: flush_handlers
