Use service. It is compatible in most use cases.
Service - Controls services on remote hosts. Supported init systems include BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart.
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_module.html
---
- hosts: localhost
gather_facts: false
become: true
tasks:
- name: Ensure nginx is installed
apt:
name: nginx
state: latest
- name: service started
service:
name: nginx
state: started
- name: service stopped
service:
name: nginx
state: stopped
sysvinit - Controls services on target hosts that use the SysV init system.
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/sysvinit_module.html
---
- hosts: localhost
gather_facts: false
become: true
tasks:
- name: Ensure nginx is installed
apt:
name: nginx
state: latest
- name: sysvinit started
sysvinit:
name: nginx
state: started
- name: sysvinit stopped
sysvinit:
name: nginx
state: stopped
Systemd Requirements: A system managed by systemd.
https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html
---
- hosts: localhost
gather_facts: false
become: true
tasks:
- name: Ensure nginx is installed
apt:
name: nginx
state: latest
# this one fails
- name: systemd started - fail
systemd:
name: nginx
state: started
# this one fails
- name: systemd stopped - fail
systemd:
name: nginx
state: stopped