main.yml 2.8 KB

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