main.yml 2.7 KB

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