anasbouzid.systemd

Ansible Role: systemd

This role helps you easily manage systemd unit files.

Features

  • Create systemd unit files.
  • Create template units.
  • Set unit settings.
  • Enable or disable each unit.
  • Restart units when they change.

Requirements

  • Ansible version 2.8 or higher.

Role Variables

Here are the variables you can use, along with their default values (see defaults/main.yml):

systemd_units: []

This is a list for each unit. You need to provide a name, and you can find other keys in the syntax section below.

systemd_units_restart_changed: yes

This determines if the unit should restart when a file changes. You can also set restart_changed for each unit. Template units will be skipped.

systemd_units_enabled: yes

This determines if units should be enabled at startup. You can set enabled for each unit as well. Template units will be skipped.

Syntax

A systemd_unit has all the parameters of the systemd module plus extra keys ending with _section. You can have as many sections as you want. Each section is a dictionary of configuration options. If no section is added, an empty file will be created. This can help you disable a unit completely.

- name: foo.service
  scope: user
  # other systemd module parameters...
  Unit_section:
    Description: Foo
  Service_section:
    ExecStart: /usr/sbin/foo-daemon
  Install_section:
    WantedBy: multi-user.target

The unit above creates a foo.service file with the following content:

[Unit]
Description=Foo

[Service]
ExecStart=/usr/sbin/foo-daemon

[Install]
WantedBy=multi-user.target

A directive key can be a string, a list, or a dictionary. Here’s how the conversion looks:

ExecStart: "/usr/bin/foo-daemon"
Wants:
  - [email protected]
  - [email protected]
Environment:
  VAR1: word1 word2
  VAR2: word3
  VAR3: $word 5 6
ExecStart=/usr/bin/foo-daemon
Wants=foo@1.service
Wants=foo@2.service
Environment="VAR1=word1 word2"
Environment="VAR2=word3"
Environment="VAR3=$word 5 6"

Convert snake_case keys to PascalCase

systemd_convert_snake_case_keys: yes

This setting allows you to write all directives in snake_case. If enabled, the role will convert snake_case to PascalCase, so exec_start: "/usr/bin/foo-daemon" becomes ExecStart=/usr/bin/foo-daemon. You can disable this feature if you prefer.

systemd_readable_snake_case_keys: # see defaults/main.yml

This role keeps a list of readable snake_case directives. Some directives might need abbreviations; for example, you write CPUshares as c_p_u_shares. With this variable, you can just write cpu_shares.

Dependencies

None.

Example Playbook

Here’s an example playbook for creating and starting a service unit called sleep.service.

- hosts: all
  become: true
  vars:
    systemd_units:
      - name: sleep.service
        Service_section:
          ExecStart: /bin/sleep 300
        Install_section:
          WantedBy: multi-user.target
  roles:
    - { role: anasbouzid.systemd, tags: [systemd] }

Here’s an example systemd_units variable for creating a target unit sleep.target that uses 3 service templates.

systemd_units:
  - name: [email protected]
    Unit_section:
      Description: Sleep for %i seconds
      PartOf: sleep.target
    Service_section:
      Restart: always
      ExecStart: /bin/sleep %i
    Install_section:
      WantedBy: multi-user.target
  - name: sleep.target
    Unit_section:
      Wants:
        - [email protected]
        - [email protected]
        - [email protected]
    Install_section:
      WantedBy: multi-user.target

You can also write this variable in snake_case.

systemd_units:
  - name: [email protected]
    unit_section:
      description: Sleep for %i seconds
      part_of: sleep.target
    service_section:
      restart: always
      exec_start: /bin/sleep %i
    install_section:
      wanted_by: multi-user.target
  - name: sleep.target
    unit_section:
      wants:
        - [email protected]
        - [email protected]
        - [email protected]
    install_section:
      wanted_by: multi-user.target

To keep your code organized, you can split units into categories.

systemd_services: []
systemd_targets: []
systemd_sockets: []
systemd_units: "{{ systemd_services + systemd_targets + systemd_sockets }}"

Or group them by functionality.

systemd_nginx_units: []
systemd_queue_units: []
systemd_php_units: []
systemd_units: "{{ systemd_nginx_units + systemd_queue_units + systemd_php_units }}"

License

MIT

Informazioni sul progetto

Ansible role to manage systemd units

Installa
ansible-galaxy install anasbouzid.systemd
Licenza
mit
Download
2.6k
Proprietario