1- name: skip unsupported distros
2  meta: end_host
3  when: ansible_distribution in ['Alpine']
4
5- name: install the test daemon script
6  copy:
7    src: ansible_test_service.py
8    dest: /usr/sbin/ansible_test_service
9    mode: '755'
10
11- name: rewrite shebang in the test daemon script
12  lineinfile:
13    path: /usr/sbin/ansible_test_service
14    line: "#!{{ ansible_python_interpreter | realpath }}"
15    insertbefore: BOF
16    firstmatch: yes
17
18- block:
19    # determine init system is in use
20    - name: detect sysv init system
21      set_fact:
22        service_type: sysv
23      when:
24        - ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux']
25        - ansible_distribution_version is version('6', '>=')
26        - ansible_distribution_version is version('7', '<')
27    - name: detect systemd init system
28      set_fact:
29        service_type: systemd
30      when: (ansible_distribution in ['RedHat', 'CentOS', 'ScientificLinux'] and ansible_distribution_major_version is version('7', '>=')) or ansible_distribution == 'Fedora' or (ansible_distribution == 'Ubuntu' and ansible_distribution_version is version('15.04', '>=')) or (ansible_distribution == 'Debian' and ansible_distribution_version is version('8', '>=')) or ansible_os_family == 'Suse'
31    - name: detect upstart init system
32      set_fact:
33        service_type: upstart
34      when:
35        - ansible_distribution == 'Ubuntu'
36        - ansible_distribution_version is version('15.04', '<')
37    - name: detect rc init system
38      set_fact:
39        service_type: rc
40      when:
41        - ansible_distribution.lower().endswith('bsd')
42
43
44    - name: display value of ansible_service_mgr
45      debug:
46        msg: 'ansible_service_mgr: {{ ansible_service_mgr }}'
47
48    - name: setup test service script
49      include_tasks: '{{ service_type }}_setup.yml'
50
51    - name: execute tests
52      import_tasks: tests.yml
53
54  always:
55    - name: disable and stop ansible test service
56      service:
57        name: ansible_test
58        state: stopped
59        enabled: false
60
61    # cleaning up changes made by this playbook
62    - include_tasks: '{{ service_type }}_cleanup.yml'
63