# What is the best way to test (verify existence) for a systemd user service?

**URL:** <https://forum.ansible.com/t/what-is-the-best-way-to-test-verify-existence-for-a-systemd-user-service/42591>\
**Category:** Get Help\
**Tags:** service, systemd\
**Created:** [May 11, 2025, 6:12pm UTC](https://forum.ansible.com/t/what-is-the-best-way-to-test-verify-existence-for-a-systemd-user-service/42591 "2025-05-11T18:12:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/daks/32/5277_2.png) [@daks](https://forum.ansible.com/u/daks)\
**Post date:** [May 11, 2025, 6:12pm UTC](https://forum.ansible.com/t/what-is-the-best-way-to-test-verify-existence-for-a-systemd-user-service/42591/1 "2025-05-11T18:12:47Z")

</div>

I have a role task which executes a script to install a systemd user (`systemctl --user`) service. I want to add a `when` condition to only execute the script when the service is not already installed.  
I’m looking for the best solution to do it.

What I’ve tried:

1. check with `service`

```auto
become: yes
become_user: user
ansible.builtin.service:
  name: my_service
check_mode: true
register: service_exist

[...]
when: not (service_exist is defined and service_exist | length > 0)

```

but it doesn’t work, service\_exist is always a dict with a lot of information even when the service doesn’t exist and I don’t clearly see what property is different between an existing service and a non-existing one

1. check with `systemd_service` and `scope: user`

```auto
become: yes
become_user: user
ansible.builtin.systemd_service:
  name: service
  state: started
  scope: user
check_mode: true
register: systemd_exist

[...]
when: systemd_exist

```

but this module fails when the service do not exist

```auto
fatal: [debian-12]: FAILED! => {"changed": false, "msg": "Could not find the requested service my_fake_service: host"}

```

Of course, i could simply test the existence of `~$user/.config/systemd/$service.service` file but I would like to do it properly (and not need to retrieve the home directory before testing this)

Any idea?

---

<div class="post-metadata">

**Author:** ![wayt](https://avatars.discourse-cdn.com/v4/letter/w/3be4f8/32.png) [@wayt](https://forum.ansible.com/u/wayt)\
**Post date:** [May 11, 2025, 8:50pm UTC](https://forum.ansible.com/t/what-is-the-best-way-to-test-verify-existence-for-a-systemd-user-service/42591/2 "2025-05-11T20:50:29Z")

</div>

You can set ignore\_errors to true and handle it later with register and then a when condition of “is failed.”

---

<div class="post-metadata">

**Author:** ![russoz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/russoz/32/127_2.png) [@russoz](https://forum.ansible.com/u/russoz)\
**Post date:** [May 12, 2025, 8:43am UTC](https://forum.ansible.com/t/what-is-the-best-way-to-test-verify-existence-for-a-systemd-user-service/42591/3 "2025-05-12T08:43:55Z")

</div>

You could also try using the `block/rescue` construct, it works like a `try/except` in Python. Something like:

```yaml
block:
  - become: yes
    become_user: user
    ansible.builtin.systemd_service:
      name: service
      state: started
      scope: user
    check_mode: true
rescue:
  # You can use `ansible_failed_result` to parse the error message from the task, no need to `register` (unless you use it elsewhere)
  - name: run the script to install the service
    # whatever you use for that

```

See more about that in:

> **[Blocks — Ansible Community Documentation](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_blocks.html#block-error-handling)**

---

<div class="post-metadata">

**Author:** ![bcoca](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/bcoca/32/9269_2.png) [@bcoca](https://forum.ansible.com/u/bcoca)\
**Post date:** [May 12, 2025, 2:35pm UTC](https://forum.ansible.com/t/what-is-the-best-way-to-test-verify-existence-for-a-systemd-user-service/42591/4 "2025-05-12T14:35:14Z")

</div>

You can also use [https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service\_facts\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/service_facts_module.html)

---

<div class="post-metadata">

**Author:** ![daks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/daks/32/5277_2.png) [@daks](https://forum.ansible.com/u/daks)\
**Post date:** [May 17, 2025, 4:35pm UTC](https://forum.ansible.com/t/what-is-the-best-way-to-test-verify-existence-for-a-systemd-user-service/42591/6 "2025-05-17T16:35:24Z")

</div>

@wayt @russoz I find those solutions a little bit ‘hacky’, the idea of try/except doesn’t looks fine for me in such situations where I only want to test a condition and get a true/false result.  
It seems that Ansible doesn’t provide a proper solution, but then I prefer to use a less-than-ideal-one (see below)  
( **edit:** not that hacky, because `ignore_errors` is configurable by task)

@bcoca I think those facts only works with systemd services on system scope, not on user one.

I think the best (in my case) solutions are:

- verify the existence of the service systemd definition file, like `~/.config/systemd/$service.service`
- verify the real execution of the service with something a `ps | grep` with `become_user`. Not sure if I need to use `ansible.builtin.shell` or `ansible.builtin.command` but I’ll look and try

---

<div class="post-metadata">

**Author:** ![russoz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/russoz/32/127_2.png) [@russoz](https://forum.ansible.com/u/russoz)\
**Post date:** [May 19, 2025, 9:16am UTC](https://forum.ansible.com/t/what-is-the-best-way-to-test-verify-existence-for-a-systemd-user-service/42591/7 "2025-05-19T09:16:30Z")

</div>

Hi @daks you’re absolutely right, and as you pointed out that `service_facts` does not handle “user” services, I am thinking of taking a look at the code and see if we can add it. Would be willing to do the honours and submit the contribution yourself? I 'd be more than happy to help.

---

<div class="post-metadata">

**Author:** ![daks](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/daks/32/5277_2.png) [@daks](https://forum.ansible.com/u/daks)\
**Post date:** [May 19, 2025, 4:09pm UTC](https://forum.ansible.com/t/what-is-the-best-way-to-test-verify-existence-for-a-systemd-user-service/42591/8 "2025-05-19T16:09:01Z")

</div>

I’m not sure I have the skill and be able to find time to do it, but it could be interesting to try 🙂
