How to Restart a Service on Ubuntu With Ansible

This blog post will show you how to restart a service on Ubuntu Linux using Ansible built-in module for service management.


By default, any Ansible installation comes with the service module, and it is also part of the Ansible-base set of modules. Using the service module, we can manage and control the following service states:

  • reloaded
  • restarted
  • started
  • stopped

We can also configure the service to auto-start or be in a stop state when the server comes up using the enable key.


In the following configuration, restart the Nginx service on Ubuntu Server 20.04 and set the service to auto-start.

- hosts: servers
    - name: restart nginx
         name: nginx
         state: restarted
         enabled: yes

Success! You're on the list.