# When condition help - Skip if service running

**URL:** <https://forum.ansible.com/t/when-condition-help-skip-if-service-running/37039>\
**Category:** Ansible Project\
**Created:** [August 26, 2022, 2:17pm UTC](https://forum.ansible.com/t/when-condition-help-skip-if-service-running/37039 "2022-08-26T14:17:12Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [August 26, 2022, 2:17pm UTC](https://forum.ansible.com/t/when-condition-help-skip-if-service-running/37039/1 "2022-08-26T14:17:12Z")

</div>

Im trying to figure out a way, on how to make a condition exit, if a service is running.  
If it isnt running, then install it.

My play book looks like below:

- name: Check if LogRhythm System Monitor service is installed  
win\_service:  
name: lg  
register: lg

- ansible.builtin.debug:  
#msg: “state: running found”  
var: lg  
#verbosity: 4  
when: lg is search(“runing”)

I purposely used the variable “runing” instead of “running”, as for new server builds, the service isnt installed and running, so it will install it.

But if the service is “running”, how can I modify my existing when condition to skip, if service is “running”?

Thanks.

---

<div class="post-metadata">

**Author:** ![Paul\_Manno](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/paul_manno/32/2804_2.png) [@Paul\_Manno](https://forum.ansible.com/u/Paul_Manno)\
**Post date:** [August 26, 2022, 2:22pm UTC](https://forum.ansible.com/t/when-condition-help-skip-if-service-running/37039/2 "2022-08-26T14:22:10Z")

</div>

[https://docs.ansible.com/ansible/latest/collections/ansible/windows/win\_service\_info\_module.html](https://docs.ansible.com/ansible/latest/collections/ansible/windows/win_service_info_module.html)

---

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [August 26, 2022, 2:27pm UTC](https://forum.ansible.com/t/when-condition-help-skip-if-service-running/37039/3 "2022-08-26T14:27:53Z")

</div>

Thanks, but I have other tasks that depend on my when condition, so I need to get it right.

---

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [August 26, 2022, 3:53pm UTC](https://forum.ansible.com/t/when-condition-help-skip-if-service-running/37039/4 "2022-08-26T15:53:46Z")

</div>

How can I search for this exact string using my example:

“exists”: true

My when condition:

when: lg is search( “exists”: true )

It errors out using “exists”: true.

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 26, 2022, 3:58pm UTC](https://forum.ansible.com/t/when-condition-help-skip-if-service-running/37039/5 "2022-08-26T15:58:57Z")

</div>

Please read the module page for a description of what is “registered” with win\_service.

Walter

---

<div class="post-metadata">

**Author:** ![Nitrous](https://avatars.discourse-cdn.com/v4/letter/n/dfb087/32.png) [@Nitrous](https://forum.ansible.com/u/Nitrous)\
**Post date:** [August 26, 2022, 4:13pm UTC](https://forum.ansible.com/t/when-condition-help-skip-if-service-running/37039/6 "2022-08-26T16:13:01Z")

</div>

Yes, its the same information I gathered previously.  
I need to search for this string in my when condition:

“exists”: true

How can I do that?

Thanks

---

<div class="post-metadata">

**Author:** ![Walter\_Rowe](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/walter_rowe/32/4506_2.png) [@Walter\_Rowe](https://forum.ansible.com/u/Walter_Rowe)\
**Post date:** [August 26, 2022, 4:21pm UTC](https://forum.ansible.com/t/when-condition-help-skip-if-service-running/37039/7 "2022-08-26T16:21:47Z")

</div>

Think it through … what is the structure of the data in ‘lg’ after win\_service? You have to learn to decipher structured data so you can learn how to reference it.

Walter
