123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- ---
- - name: create parent folder for unit files
- ansible.builtin.file:
- dest: ~/.config/systemd/user
- state: directory
- when: systemd_unit_scope == 'user'
- register: _config_dir
- - name: 'configure systemd unit {{ systemd_unit_name }}'
- copy:
- dest: '{{ _config_dir_path }}/{{ systemd_unit_name }}'
- content: |
- # ansible managed
- {{ systemd_unit_config }}
- mode: a=r
- vars: ©_config_file_vars
- _config_dir_path: >-
- {{ _config_dir.path if systemd_unit_scope == 'user'
- else '/etc/systemd/' + (
- "user" if systemd_unit_scope == "global" else systemd_unit_scope
- ) }}
- register: _config_file
- when: systemd_unit_config
- - name: 'create parent folder for drop-in config files of systemd unit {{ systemd_unit_name }}'
- file:
- path: '{{ _config_dir_path }}/{{ systemd_unit_name }}.d'
- state: directory
- mode: u=rwx,go=rx
- vars: *copy_config_file_vars
- register: _dropin_config_dir
- when: systemd_unit_dropin_config is defined
- - name: 'configure systemd unit {{ systemd_unit_name }} via drop-in config'
- copy:
- # > Along with a unit file foo.service, a "drop-in" directory foo.service.d/ may
- # > exist. All files with the suffix ".conf" from this directory will be parsed
- # > after the unit file itself is parsed.
- dest: '{{ _dropin_config_dir.path }}/{{ systemd_unit_dropin_config_name }}.conf'
- content: |
- # ansible managed
- {{ systemd_unit_dropin_config }}
- mode: a=r
- register: _dropin_config_file
- when: systemd_unit_dropin_config is defined
- - name: set return values
- set_fact:
- systemd_unit_config_changed: >-
- {{ _config_file.changed or _dropin_config_file.changed }}
- - name: 'fetch state of systemd unit {{ systemd_unit_name }}'
- systemd:
- scope: '{{ systemd_unit_scope }}'
- name: '{{ systemd_unit_name }}'
- register: _unit
- when: systemd_unit_scope in ('system', 'user')
- and (systemd_unit_config_changed or systemd_unit_restart_if_active)
- - name: 'set state of systemd unit {{ systemd_unit_name }}'
- systemd:
- scope: '{{ systemd_unit_scope }}'
- daemon_reload: '{{ systemd_unit_config_changed }}'
- name: '{{ systemd_unit_name }}'
- enabled: '{{ systemd_unit_enabled | default(omit) }}'
- # > `started'/`stopped' are idempotent actions [...]
- # > `restarted' will always bounce the service
- state: >-
- {{ "restarted" if (systemd_unit_state is not defined
- or systemd_unit_state != "stopped")
- and (systemd_unit_config_changed
- or systemd_unit_restart_if_active)
- and 'ActiveState' in _unit.status
- and _unit.status.ActiveState == "active"
- else systemd_unit_state | default(omit) }}
- when: >-
- systemd_unit_scope in ('system', 'user')
- and (systemd_unit_config_changed
- or systemd_unit_enabled is defined
- or systemd_unit_state is defined
- or (systemd_unit_restart_if_active
- and 'ActiveState' in _unit.status
- and _unit.status.ActiveState == "active")
- )
|