# when condition is not working

**URL:** <https://forum.ansible.com/t/when-condition-is-not-working/35810>\
**Category:** Ansible Project\
**Created:** [October 1, 2021, 12:10am UTC](https://forum.ansible.com/t/when-condition-is-not-working/35810 "2021-10-01T00:10:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![subbamma\_natla](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@subbamma\_natla](https://forum.ansible.com/u/subbamma_natla)\
**Post date:** [October 1, 2021, 12:10am UTC](https://forum.ansible.com/t/when-condition-is-not-working/35810/1 "2021-10-01T00:10:14Z")

</div>

can somebody explaine me why service sis not starting

on mhost1:

[root@mhost1 ~]# ps -ef | grep -v grep | grep httpd  
[root@mhost1 ~]#

---

<div class="post-metadata">

**Author:** ![Gajendra\_Gaikwad](https://avatars.discourse-cdn.com/v4/letter/g/9fc29f/32.png) [@Gajendra\_Gaikwad](https://forum.ansible.com/u/Gajendra_Gaikwad)\
**Post date:** [October 1, 2021, 11:04am UTC](https://forum.ansible.com/t/when-condition-is-not-working/35810/2 "2021-10-01T11:04:58Z")

</div>

Hi,

change to  
when: htop\_is\_running.stdout == ‘0’

---

<div class="post-metadata">

**Author:** ![Sandip\_Bhattacharya](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Sandip\_Bhattacharya](https://forum.ansible.com/u/Sandip_Bhattacharya)\
**Post date:** [October 1, 2021, 11:53am UTC](https://forum.ansible.com/t/when-condition-is-not-working/35810/3 "2021-10-01T11:53:31Z")

</div>

It might be cleaner to use pgrep instead for process check. Then you can use something like this:

&nbsp;&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: Process check  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;command: pgrep httpd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: httpd\_check  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ignore\_errors: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- debug:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var: httpd\_check.failed

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;- name: restart httpd if down  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;systemd:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: httpd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: restarted  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: httpd\_check.failed

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [October 1, 2021, 12:05pm UTC](https://forum.ansible.com/t/when-condition-is-not-working/35810/4 "2021-10-01T12:05:05Z")

</div>

What's wrong with asserting that the service should be started?

- systemd:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: httpd  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: started

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Sandip\_Bhattacharya](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Sandip\_Bhattacharya](https://forum.ansible.com/u/Sandip_Bhattacharya)\
**Post date:** [October 1, 2021, 1:14pm UTC](https://forum.ansible.com/t/when-condition-is-not-working/35810/5 "2021-10-01T13:14:09Z")

</div>

> What's wrong with asserting that the service should be started?
> 
> - systemd:  
> name: httpd  
> state: started

For the vast majority of cases that should work (the apache service in major  
distros being such a case).

Sometimes you do get an edge case where the PID check in the unit file is not a  
good enough check for the health of the service as a whole.

- Sandip

---

<div class="post-metadata">

**Author:** ![subbamma\_natla](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@subbamma\_natla](https://forum.ansible.com/u/subbamma_natla)\
**Post date:** [October 1, 2021, 1:32pm UTC](https://forum.ansible.com/t/when-condition-is-not-working/35810/6 "2021-10-01T13:32:46Z")

</div>

when: htop\_is\_running.stdout == ‘0’ did not work

command: pgrep httpd

My question is why the following not working  
when: status\_httpd.stdout\_lines == 0

ok: [mhost1] =\> {  
“status\_httpd.stdout\_lines”: [  
“0”  
]  
}

Thanks

---

<div class="post-metadata">

**Author:** ![Sandip\_Bhattacharya](https://avatars.discourse-cdn.com/v4/letter/s/a88e4f/32.png) [@Sandip\_Bhattacharya](https://forum.ansible.com/u/Sandip_Bhattacharya)\
**Post date:** [October 1, 2021, 1:36pm UTC](https://forum.ansible.com/t/when-condition-is-not-working/35810/7 "2021-10-01T13:36:50Z")

</div>

If the 'when' condition, you are effectively working in python land.

Here you are making probably two mistakes:  
&nbsp;&nbsp;- 'stdout\_lines' is an array. 'stdout' is a string.  
&nbsp;&nbsp;- you are comparing strings to numbers

---

<div class="post-metadata">

**Author:** ![subbamma\_natla](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@subbamma\_natla](https://forum.ansible.com/u/subbamma_natla)\
**Post date:** [October 1, 2021, 2:11pm UTC](https://forum.ansible.com/t/when-condition-is-not-working/35810/8 "2021-10-01T14:11:52Z")

</div>

sorry. I take it back my words

the following change is working

when: htop\_is\_running.stdout == ‘0’

Thanks
