The service module is used to start, stop, restart, reload, enable and disable init services on the managed node (e.g. the target system). Or, the local_action module can be used to invoke this module on the control node (that's your Ansible server). This is similar to using the chkconfig command. The become module is used to run the command as root.
The ansible-doc service command can be used to show the Ansible documention on the service module.
In this example, the nginx web server will be stopped and disabled.
- name: stop nginx service: name: nginx state: stopped enabled: no become: yes become_user: root
In this example, the nginx web server will be started and enabled.
- name: stop nginx service: name: nginx state: started enabled: yes become: yes become_user: root
If the service was successfully stopped, started, restated, or reloaded, the play should indicate changed.
TASK [stop nginx] changed: [server1.example.com]
Additional arguments can be included. For example, with the service command, here is how you would include the "foo" argument.
service nginx start foo
And here is how to do the same in Ansible.
- name: stop nginx service: name: nginx state: started enabled: yes arguments: foo become: yes become_user: root