# Need help on using a variable in when condition

**URL:** <https://forum.ansible.com/t/need-help-on-using-a-variable-in-when-condition/33027>\
**Category:** Ansible Project\
**Created:** [May 26, 2020, 5:37pm UTC](https://forum.ansible.com/t/need-help-on-using-a-variable-in-when-condition/33027 "2020-05-26T17:37:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pandu\_jh](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@Pandu\_jh](https://forum.ansible.com/u/Pandu_jh)\
**Post date:** [May 26, 2020, 5:37pm UTC](https://forum.ansible.com/t/need-help-on-using-a-variable-in-when-condition/33027/1 "2020-05-26T17:37:15Z")

</div>

I have a below task in my playbook. It checks the desired count of the process specified in vars.  
If a task returned 50 value and since it has 0. my condition gets satisfied which is incorrect.

vars:

- sap\_process\_count: “0”  
tasks:

- name: Pre Check - Verifying app status  
shell: ps -ef | grep sap | grep -v grep | wc -l  
register: prechk\_process\_count  
ignore\_errors: yes

- debug:  
msg: “application is already stopped in {{ inventory\_hostname }}”  
when: “sap\_process\_count in prechk\_process\_count.stdout”

I tried with the condition "prechk\_process\_count.stdout == “0"” . it’s working fine  
my question, Is it possible to define the process count “0” through a variable in "prechk\_process\_count.stdout == “0"” condition.

Please advise.

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [May 26, 2020, 6:56pm UTC](https://forum.ansible.com/t/need-help-on-using-a-variable-in-when-condition/33027/2 "2020-05-26T18:56:52Z")

</div>

That looks fragile. Instead of chaining a load of shell commands and hoping for the best, why don’t you use the available system utilities that are actually designed to manage starting and stopping applications?

See [https://docs.ansible.com/ansible/latest/modules/systemd\_module.html](https://docs.ansible.com/ansible/latest/modules/systemd_module.html)
