main.yml 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. - name: 'configure systemd unit {{ systemd_unit_name }}'
  2. copy:
  3. dest: '/etc/systemd/system/{{ systemd_unit_name }}'
  4. content: |
  5. # ansible managed
  6. {{ systemd_unit_config }}
  7. mode: a=r
  8. register: _config_file
  9. when: systemd_unit_config is defined
  10. - name: 'configure systemd unit {{ systemd_unit_name }} via drop-in config'
  11. copy:
  12. # > Along with a unit file foo.service, a "drop-in" directory foo.service.d/ may
  13. # > exist. All files with the suffix ".conf" from this directory will be parsed
  14. # > after the unit file itself is parsed.
  15. dest: '/etc/systemd/system/{{ systemd_unit_name }}.d/override.conf'
  16. content: |
  17. # ansible managed
  18. {{ systemd_unit_dropin_config }}
  19. mode: a=r
  20. register: _dropin_config_file
  21. when: systemd_unit_dropin_config is defined
  22. - name: 'fetch state of systemd unit {{ systemd_unit_name }}'
  23. systemd:
  24. name: '{{ systemd_unit_name }}'
  25. register: _unit
  26. when: _config_file.changed or _dropin_config_file.changed or systemd_unit_restart_if_active
  27. - name: 'set state of systemd unit {{ systemd_unit_name }}'
  28. systemd:
  29. daemon_reload: '{{ _config_file.changed or _dropin_config_file.changed }}'
  30. name: '{{ systemd_unit_name }}'
  31. enabled: '{{ systemd_unit_enabled | default(omit) }}'
  32. # > `started'/`stopped' are idempotent actions [...]
  33. # > `restarted' will always bounce the service
  34. state: >-
  35. {{ "restarted" if (systemd_unit_state is not defined
  36. or systemd_unit_state != "stopped")
  37. and (_config_file.changed
  38. or _dropin_config_file.changed
  39. or systemd_unit_restart_if_active)
  40. and 'ActiveState' in _unit.status
  41. and _unit.status.ActiveState == "active"
  42. else systemd_unit_state | default(omit) }}
  43. when: >-
  44. _config_file.changed
  45. or _dropin_config_file.changed
  46. or systemd_unit_enabled is defined
  47. or systemd_unit_state is defined
  48. or (systemd_unit_restart_if_active
  49. and 'ActiveState' in _unit.status
  50. and _unit.status.ActiveState == "active")