7

The systemd module: https://docs.ansible.com/ansible/latest/collections/ansible/builtin/systemd_module.html

I'm looking for a way to add a Condition to the service file.

For instance:

ConditionPathIsMountPoint=/mnt/myreplication/path/

This would be useful for docker installations, ensuring docker doesn't start containers before a mount they need is actually available.

Sadly, it looks like Ansible doesn't support adding this right now. Am I correct there? Will I need to manually add it, or with lineinfile? Or is there an other way?

EDIT: This question appears to be getting views, so I'll add this:

https://askubuntu.com/questions/659267/how-do-i-override-or-configure-systemd-services

And this answer to another question of mine: https://askubuntu.com/a/1348117/1612

To quote it:

Don't edit files in /lib/systemd/ or /usr/share/systemd as they will get overwritten on updates.

KdgDev
  • 14,299
  • 46
  • 120
  • 156

3 Answers3

10

Let me post a solution using ini_file that worked for me:

- name: Create a foo.service override directory
  file:
    owner: root
    group: root
    mode: 0755
    path: /etc/systemd/system/foo.service.d
    state: directory
- name: Set up foo.service override
  ini_file:
    dest: /etc/systemd/system/foo.service.d/bar_override.conf
    owner: root
    group: root
    mode: 0644
    section: Unit
    option: ConditionPathIsMountPoint
    value: /mnt/myreplication/path/

This avoids rewriting the original service file, but rather adds a dedicated override into a .d subdirectory.

Note that ini_file adds whitespace around = as in

[Unit]
ConditionPathIsMountPoint = /mnt/myreplication/path/

but this is fine, see systemd.syntax(7):

Each file is a plain text file divided into sections, with configuration entries in the style key=value. Whitespace immediately before or after the "=" is ignored.

Petr
  • 62,528
  • 13
  • 153
  • 317
4

Am I correct there?

Right, the systemd_module is not for manipulating service files.

Since I've had some similar questions in the past I like to share my approach.

You could either maintain your own service file template and deploy it

- name: "Make sure the systemd service file is correct"
  template:
    src: "{{ MYSERVICE }}.service.j2"
    dest: "/etc/systemd/system/{{ MYSERVICE }}.service"
    mode: 0755
  tags: install,systemd

or add the necessary line via lineinfile_module

- name: "Make sure the entry in '{{ MYSERVICE }}.service' exists"
  lineinfile:
    path: "/etc/systemd/system/{{ MYSERVICE }}.service"
    line: "ConditionPathIsMountPoint=/mnt/myreplication/path/"
    state: present
  tags: install,systemd

and reload and restart the service

- name: "Make sure the service is started and enabled via systemd"
  systemd:
    name: "{{ MYSERVICE }}"
    state: started
    enabled: yes
    daemon_reload: yes
  tags: install,systemd

whereby it might be good to use insertbefore or insertafter also.

U880D
  • 8,601
  • 6
  • 24
  • 40
  • 3
    Is there any reason to use `lineinfile` rather than [`ini_file`](https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html) to deal with files in place ? – Zeitounator Dec 01 '20 at 16:31
  • @Zeitounator, the [ini_file](https://docs.ansible.com/ansible/latest/collections/community/general/ini_file_module.html) module can deal with structured files and sections `[section]`, which `lineinfile` isn't doing. – U880D Dec 01 '20 at 18:09
  • 3
    Well since this is exactly what a systemd service file looks like, it seems a more appropriate solution doesn't it ? And that totally wipes the need to use insertbefore of insertafter. – Zeitounator Dec 01 '20 at 18:11
  • @Zeitounator, right, good point. I think I will do a test with that too and update the answer accordingly. – U880D Dec 01 '20 at 19:16
  • 1
    @Zeitounator Nice, I wasn't aware of the ini_file module. And the answer itself also looks good. I'll have a look at this. It seems I can only notify 1 user at a time. – KdgDev Dec 02 '20 at 14:28
  • @Zeitounator Looks like "option" has to be unique. Or gets overwritten if there has to be more than 1. Whereas I believe systemd service files support a condition being used more than once: https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Conditions%20and%20Asserts – KdgDev Dec 02 '20 at 20:59
  • So if I want to check multiple mount points, that's what I need and ini_file doesn't support it. – KdgDev Dec 02 '20 at 21:09
1

EDIT: This question appears to be getting views, so I'll add this:

https://askubuntu.com/questions/659267/how-do-i-override-or-configure-systemd-services

And this answer to another question of mine: https://askubuntu.com/a/1348117/1612

To quote it:

Don't edit files in /lib/systemd/ or /usr/share/systemd as they will get overwritten on updates.

KdgDev
  • 14,299
  • 46
  • 120
  • 156