Error- When Condition

Hello Team,

Hope you all are doing well.

Here is my case:

Tasks > remove.yml

- 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:

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:

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

---
# 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"

Use *pkill*
https://www.commandlinux.com/man-page/man1/pkill.1.html

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

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

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

when: “RUNNING” in job_check.stdout_lines

Hi,

Don't you need to put "RUNNING" in quotes? It is a string, no?

  when: "RUNNING" in job_check.stdout_lines

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

  when: '"RUNNING" in job_check.stdout_lines'

Alternatively you could write some multiline string:

  when: >-
    "RUNNING" in job_check.stdout_lines

Cheers,
Felix

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.

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:

    when: "ok" in result.stdout

Could be written as:

   when: '"ok" in result.stdout'

Or equivalently:

   when: "'ok' in result.stdout"

Cheers,
Felix

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.

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’

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:

Example:

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”)

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

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