main.yml 2.4 KB

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