Ansible

How to control Systemd with Ansible

Managing services is one of the fundamental requirements for any user. With tools such as Systemd powering up all major Linux distributions, it is critical to understand how to manage services.

This tutorial will teach you to use the Ansible systemd module to start, stop, and manage services on any host using the Systemd Init service.

Setting Up Ansible

The first step is to install and configure Ansible on your system. In this example, we run Ansible on Debian 11 system.

Start by updating the software repositories and installing Ansible:

sudo apt-get update
sudo apt-get install ansible -y

Once installed, we can set up the remote hosts to be managed using the Ansible control node.

Configure Ansible Inventory

Open the terminal and edit the inventory file as shown in the command below:

sudo vim /etc/ansible/hosts

In the host inventory file, add the IP address of the remote hosts you wish to manage using Ansible.

Setup SSH Key

The next step is to create an SSH key pair. This allows you to log in to the remote host from your Ansible control node without the need to type a password.

Start by generating an SSH key using the command:

$ ssh-keygen

Follow the prompts of the ssh-keygen command to generate a public and private key pair.

Once completed, use the ssh-copy-id command to copy your SSH public key to the remote host.

An example command is as shown:

ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@192.168.100.17

Replace the username and password with your remote host’s remote user and IP address.

Next, enter the password to log in to the remote host and upload your SSH key pair.

Once completed, SSH into the remote host, and you will be logged in without a password prompt.

Managing Services with Systemd

To manage the services on a remote host using the systemd module, ensure the Systemd service and system manager manage the remote host.

The systemd module is part of Ansible core and is available in all Ansible installations by default.

Let us look at a few examples of using the systemd module to manage services on the remote hosts.

Starting Services

The systemd module in Ansible is straightforward to use. For example, to start a service, pass the name of the service and the state you want it to be, in this case, started.

The following example playbook shows how to start a service using the systemd module.

---
- name: Ansible start service
  hosts: all
  gather_facts: true
  tasks:
    - name: Start a service with systemd
      systemd:
        name: apache2
        state: started

The above example playbook will start the Apache2 HTTP server using systemd.

Ensuring the target service is installed and managed by systemd on the remote host is good to avoid errors.

If the service does not exist, Ansible will throw an error as shown in the example screenshot below:

Stopping Services

When stopping services, change the state parameter to stopped as shown in the example playbook below:

---
- name: Ansible stop services
  hosts: all
  gather_facts: true
  tasks:
    - name: Stop a service with systemd
      systemd:
        name: apache2
        state: stopped

Once executed successfully, the target service on the remote host will be stopped.

Reload Services

To reload a service using systemd, set the state to reload. It is good to know that you can use the service’s full name or short as we have done in previous playbooks.

For example:

---
- name: Ansible reload a service
  hosts: all
  gather_facts: true
  tasks:
    - name: Reload services with systemd
      systemd:
        name: nginx.service
        state: reloaded

The above example will reload the Nginx service as passed in the playbook.

Enable Services

In the cases where you need a service to be started as the system boots up, you need to ensure the service is enabled.

Ansible systemd module provides you with the enabled parameter, which you can use to allow a service to start at system startup.

Consider the example playbook shown below:

---
- name: Ansible enable service
  hosts: all
  gather_facts: true
  tasks:
    - name: Enable nginx service
      systemd:
        name: nginx.service
        enabled: true
        masked: no

The playbook above enables the Nginx service and ensures that it’s not masked.

Manage Multiple Services

You can also use the with_items parameter to manage multiple services. For example, to start various services at once, we can create a playbook as:

---
- name: Ansible start services
  hosts: all
  gather_facts: true
  tasks:
    - name: Start these services
      systemd:
        name: "{{ item }}"
        state: started
      with_items:
        - apache2
        - mysql
        - redis-server

The example playbook above should restart the services in the with_items block.

Closing

This guide shows you how to manage services on remote hosts using the Systemd module in Ansible.

Thank you for reading!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list