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.

Service

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.

Configuration

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

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

Processing…
Success! You're on the list.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.