main.yml 3.0 KB

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