Chapter 26. Managing systemd units by using the systemd RHEL system role

With the systemd RHEL system role you can deploy unit files and manage systemd units on multiple systems by using the Red Hat Ansible Automation Platform.

You can use the systemd_units variable in systemd RHEL system role playbooks to gain insights into the status of systemd units on a target system. The variable displays a list of dictionaries. Each dictionary entry describes the state and configuration of one systemd unit present on the managed host. The systemd_units variable is updated as the final step of task execution and captures the state after the role has run all tasks.

26.1. Variables of the systemd RHEL system role

You can customize the behavior of the systemd system and service manager by setting the following input variables for the systemd RHEL system role:

systemd_unit_files
Specifies a list of systemd unit file names that you want to deploy to the target hosts.
systemd_unit-file_templates
Specifies a list of systemd unit file names that should be treated as templates. Each name should correspond to the Jinja template file. For example, for a <name>.service unit file, you can either have the <name>.service Jinja template file or the <name>.service.j2 Jinja template file. If your local template file has a .j2 suffix, Ansible removes the suffix before creating the final unit file name.
systemd_dropins

Specifies a list of systemd drop-in configuration files to modify or enhance the behavior of a systemd unit without making changes to the unit file directly.

When you set the systemd_dropins = <name>.service.conf variable in the playbook, Ansible takes the local <name>.service.conf file and creates a drop-in file on the managed node named always 99-override.conf and uses this drop-in file to modify the <name>.service systemd unit.

systemd_started_units
Specifies the list of unit names that systemd starts.
systemd_stopped_units
Use this variable to specify the list of unit names that systemd should stop.
systemd_restarted_units
Specifies a list of unit names that systemd should restart.
systemd_reloaded_units
Specifies a list of unit names that systemd should reload.
systemd_enabled_units
Specifies a list of unit names that systemd should enable.
systemd_disabled_units
Specifies a list of unit names that systemd should disable.
systemd_masked_units
Specifies a list of unit names that systemd should mask.
systemd_unmasked_units
Specifies a list of unit names that systemd should unmask.

Additional resources

  • /usr/share/ansible/roles/rhel-system-roles.systemd/README.md file
  • /usr/share/doc/rhel-system-roles/systemd/ directory

26.2. Deploying and starting a systemd unit by using the systemd system role

You can apply the systemd RHEL system role to perform tasks related to systemd unit management on the target hosts. You will set the systemd RHEL system role variables in a playbook to define which unit files systemd manages, starts, and enables.

Prerequisites

Procedure

  1. Create a playbook file, for example ~/playbook.yml, with the following content:

    ---
    - name: Deploy and start systemd unit
      hosts: managed-node-01.example.com
      roles:
        - rhel-system-roles.systemd
      vars:
        systemd_unit_files:
          - <name1>.service
          - <name2>.service
          - <name3>.service
        systemd_started_units:
          - <name1>.service
          - <name2>.service
          - <name3>.service
        systemd_enabled_units:
          - <name1>.service
          - <name2>.service
          - <name3>.service
  2. Validate the playbook syntax:

    $ ansible-playbook --syntax-check ~/playbook.yml

    Note that this command only validates the syntax and does not protect against a wrong but valid configuration.

  3. Run the playbook:

    $ ansible-playbook ~/playbook.yml

Additional resources

  • /usr/share/ansible/roles/rhel-system-roles.systemd/README.md file
  • /usr/share/doc/rhel-system-roles/systemd/ directory