Skip to content
Snippets Groups Projects

Webarchitects systemd Ansible role

pipeline status

An Ansible role for configuring systemd services on Debian, this role has been designed to be as generic as possible in order to enable to it be used to configure any systemd service, by default it configures systemd-timesyncd.

Role variables

See the defaults/main.yml file for the default variables, these are described below.

systemd

Set the systemd variable to false to prevent any tasks in this role being run, it defaults to true.

systemd_timesyncd_reboot

When the systemd_timesyncd_reboot variable is set to true servers which have incorrect clocks will be rebooted by this role in order to correct their clocks, it defaults to false.

systemd_units

A list of systemd units to configure, for example:

systemd_units:
  - name: systemd-timesyncd
    files:
      - path: /etc/systemd/timesyncd.conf
        comment: |
          Entries in this file show the compile time defaults.
          You can change settings by editing this file.
          Defaults can be restored by simply deleting this file.
          See timesyncd.conf(5) for details.
        conf:
          Time:
            NTP: 0.pool.ntp.org 1.pool.ntp.org 3.pool.ntp.org 2.pool.ntp.org
        state: present
    pkgs:
      - systemd-timesyncd
    state: present
    unit_state: started

The only required variables is name, see the meta/argument_specs.yml for the variable types.

For each service required .deb packages, the state of the service and the files to be created / amended and their content as YAML can be specified.

Files are read using the JC ini parser and only updated if the conf is to be changed.

Files can optionally have one of four optional states set:

  • absent - the file will be deleted.
  • edited - the existing file will be edited using the Ansible ini module.
  • present - if the file exists it will be edited using the Ansible ini module, if not it will be created using the templates/unit.j2 template.
  • templated - the file will be created if it does not exist or updated if it already exists using the templates/unit.j2 template.

If the state is not set it defaults to present.

The edited option can not remove variables and, unlike the templated option, it preserves existing comments.

When files are updated or deleted backups are created based on the existing file name but prefixed with a leading . and suffixed with a timestamp in ISO8601 format and the file extension .bak.

Read existing systemd files using JC

You can read existing systemd files as YAML on the command line using JC, for example:

cat /etc/systemd/timesyncd.conf | jc --ini -py
---
Time:
  NTP: 0.pool.ntp.org 1.pool.ntp.org 3.pool.ntp.org 2.pool.ntp.org

Dependencies

This role requires Ansible 2.13 or newer, JC and JMESPath to be installed using pip3 on the Ansible controller.

Repository

The primary URL of this repo is https://git.coop/webarch/systemd however it is also mirrored to GitHub and available via Ansible Galaxy.

If you use this role please use a tagged release, see the release notes.

License

This role is released under the same terms as Ansible itself, the GNU GPLv3.