# Error- When Condition

**URL:** <https://forum.ansible.com/t/error-when-condition/36982>\
**Category:** Ansible Project\
**Created:** [August 12, 2022, 6:01am UTC](https://forum.ansible.com/t/error-when-condition/36982 "2022-08-12T06:01:01Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![farrukh\_ahmed](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@farrukh\_ahmed](https://forum.ansible.com/u/farrukh_ahmed)\
**Post date:** [August 12, 2022, 6:01am UTC](https://forum.ansible.com/t/error-when-condition/36982/1 "2022-08-12T06:01:01Z")

</div>

Hello Team,

Hope you all are doing well.

Here is my case:

Tasks \> remove.yml

```auto
- name: "Check if any job is running"
  shell: |
    supervisorctl status "{{ job_name }}:*"
  register: job_check

- debug:
    msg: "{{ job_check.stdout_lines }}"
  notify:
    - stop job
    - remove job
  when: RUNNING in job_check.stdout_lines

```

Output:

```auto
TASK [supervisord : Check if any job is running] ****************************************************************************************************

changed: [34.204.229.0] => {
    "changed": true,
    "cmd": "supervisorctl status \"idle-queue:*\"\n",
    "delta": "0:00:00.137281",
    "end": "2022-08-12 05:46:48.807981",
    "invocation": {
        "module_args": {
            "_raw_params": "supervisorctl status \"idle-queue:*\"\n",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2022-08-12 05:46:48.670700",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "idle-queue:idle-queue_00 RUNNING pid 32292, uptime 1:21:01\nidle-queue:idle-queue_01 RUNNING pid 32293, uptime 1:21:01\nidle-queue:idle-queue_02 RUNNING pid 32291, uptime 1:21:01",
    "stdout_lines": [
        "idle-queue:idle-queue_00 RUNNING pid 32292, uptime 1:21:01",
        "idle-queue:idle-queue_01 RUNNING pid 32293, uptime 1:21:01",
        "idle-queue:idle-queue_02 RUNNING pid 32291, uptime 1:21:01"
    ]
}

```

Error:

```auto
TASK [supervisord : debug] **************************************************************************************************************************
fatal: [34.204.229.0]: FAILED! => {
    "msg": "The conditional check 'RUNNING in job_check.stdout_lines' failed. The error was: error while evaluating conditional (RUNNING in job_check.stdout_lines): 'RUNNING' is undefined\n\nThe error appears to be in '/home/farrukh/Documents/work/devrim/repositories/ansible-playbooks/roles/supervisord/tasks/remove_job.yml': line 8, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- debug:\n ^ here\n"
}

```

**What I’m trying to achieve is that;**  
**1) Check if any job process is running.**  
**2) If the job process found running then; first stop the process and then remove the process.**

PS: using handlers.

Handlers \> main.yml

```auto
---
# handlers file for supervisord

- name: "{{ action }} job"
  shell: |
    supervisorctl {{ action }}
  when: action == "update" or action == "reread"

- name: "{{ action }} job"
  shell: |
    supervisorctl {{ action }} "{{ job_name }}:*"
  when: action != "update" or action != "reread"

```

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 12, 2022, 6:48am UTC](https://forum.ansible.com/t/error-when-condition/36982/2 "2022-08-12T06:48:35Z")

</div>

Use \*pkill\*  
[https://www.commandlinux.com/man-page/man1/pkill.1.html](https://www.commandlinux.com/man-page/man1/pkill.1.html)

---

<div class="post-metadata">

**Author:** ![Nico\_Kadel-Garcia](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/nico_kadel-garcia/32/9998_2.png) [@Nico\_Kadel-Garcia](https://forum.ansible.com/u/Nico_Kadel-Garcia)\
**Post date:** [August 13, 2022, 9:58pm UTC](https://forum.ansible.com/t/error-when-condition/36982/3 "2022-08-13T21:58:02Z")

</div>

pkill is a useful tool, and a dangerous one if other processes may  
have matching names. Use it cautiously.

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [August 13, 2022, 11:40pm UTC](https://forum.ansible.com/t/error-when-condition/36982/4 "2022-08-13T23:40:19Z")

</div>

Right. Probably it would be good to add that cautious usage here  
includes running \*pgrep\* first and see whether the selected processes  
are what you really want. This is the reason why \*pgrep/pkill\* come  
together.

FWIW. See the playbook for testing asynchronous scripts and signals  
[https://gist.github.com/vbotka/755c3c4fea3d7afb54406412325a3f12](https://gist.github.com/vbotka/755c3c4fea3d7afb54406412325a3f12)

---

<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 15, 2022, 12:05pm UTC](https://forum.ansible.com/t/error-when-condition/36982/5 "2022-08-15T12:05:52Z")

</div>

Don’t you need to put “RUNNING” in quotes? It is a string, no?

when: “RUNNING” in job\_check.stdout\_lines

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [August 15, 2022, 6:40pm UTC](https://forum.ansible.com/t/error-when-condition/36982/6 "2022-08-15T18:40:44Z")

</div>

Hi,

> Don't you need to put "RUNNING" in quotes? It is a string, no?
> 
> &nbsp;&nbsp;when: "RUNNING" in job\_check.stdout\_lines

actually you need double quotes, since YAML would complain about the  
quotes not being terminated correctly:

> &nbsp;&nbsp;when: '"RUNNING" in job\_check.stdout\_lines'

Alternatively you could write some multiline string:

> &nbsp;&nbsp;when: \>-  
> &nbsp;&nbsp;&nbsp;&nbsp;"RUNNING" in job\_check.stdout\_lines

Cheers,  
Felix

---

<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 15, 2022, 7:37pm UTC](https://forum.ansible.com/t/error-when-condition/36982/7 "2022-08-15T19:37:52Z")

</div>

You do not need double quote. A ‘when’ condition does not need to be quoted.

That said a ‘when’ condition also does not do an effective “grep” to see if one string is contained in a list of strings. It will do exact matches only.

This will work.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [August 15, 2022, 7:44pm UTC](https://forum.ansible.com/t/error-when-condition/36982/8 "2022-08-15T19:44:08Z")

</div>

Hi,

> You do not need double quote. A 'when' condition does not need to be  
> quoted.

this has nothing to do with 'when' conditions, but with YAML parsing.

A statement such as

> when: "RUNNING" in job\_check.stdout\_lines

will result in a YAML parsing error. Actually the error output from  
Ansible is pretty helpful here:

> This one looks easy to fix. It seems that there is a value started  
> with a quote, and the YAML parser is expecting to see the line ended  
> with the same kind of quote. For instance:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;when: "ok" in result.stdout
> 
> Could be written as:
> 
> &nbsp;&nbsp;&nbsp;when: '"ok" in result.stdout'
> 
> Or equivalently:
> 
> &nbsp;&nbsp;&nbsp;when: "'ok' in result.stdout"

Cheers,  
Felix

---

<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 15, 2022, 8:20pm UTC](https://forum.ansible.com/t/error-when-condition/36982/9 "2022-08-15T20:20:02Z")

</div>

I agree, and placing it in quotes doesn’t fix it. I tested your suggestion and it also failed. It doesn’t like starting a ‘when’ clause with a quote in any form.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [August 15, 2022, 8:53pm UTC](https://forum.ansible.com/t/error-when-condition/36982/10 "2022-08-15T20:53:02Z")

</div>

That’s not true, I do it all the time. The raw yaml value needs to be quoted but you still need to quote the inner string value to make sure it’s interpreted as a string, e.g.

when: ‘“RUNNING” in job\_check.stdout\_lines’

---

<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 16, 2022, 11:18am UTC](https://forum.ansible.com/t/error-when-condition/36982/11 "2022-08-16T11:18:54Z")

</div>

I stand corrected. The syntax does work. I think I had a type of my own causing the error I experienced with it. Thank you for correcting me.

It still does not produce the desired result. My prior example does, and for the same reason I stated previously.

This “when” clause does not do an effective “grep” of each line in the list.

when: ‘“RUNNING” in my\_lines’

It does not find the word “RUNNING” in any of the list of strings. It will only be true of any of the lines is exactly “RUNNING” and nothing else.

This playbook:

---

<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 16, 2022, 11:22am UTC](https://forum.ansible.com/t/error-when-condition/36982/12 "2022-08-16T11:22:50Z")

</div>

Example:

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [August 16, 2022, 11:52am UTC](https://forum.ansible.com/t/error-when-condition/36982/13 "2022-08-16T11:52:06Z")

</div>

Rather than

when: RUNNING in job\_check.stdout\_lines

the canonical forms are either

when: job\_check.stdout\_lines is search(“RUNNING”)

or

when: job\_check.stdout is search(“RUNNING”)

---

<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 16, 2022, 11:58am UTC](https://forum.ansible.com/t/error-when-condition/36982/14 "2022-08-16T11:58:20Z")

</div>

Yes, that works also, and is simpler than my example. Nice!

---

<div class="post-metadata">

**Author:** ![farrukh\_ahmed](https://avatars.discourse-cdn.com/v4/letter/f/a9adbd/32.png) [@farrukh\_ahmed](https://forum.ansible.com/u/farrukh_ahmed)\
**Post date:** [August 19, 2022, 8:16am UTC](https://forum.ansible.com/t/error-when-condition/36982/15 "2022-08-19T08:16:00Z")

</div>

Thank you, gentlemen, for your precious time and efforts, in clearing all the minor doubts.

Both the solutions worked for me, that Walter and Lewis presented. But Lewis optimized this more.

It was a productive discussion though.

Thanks & regards,

FARRUKH AHMED
