Error- When Condition

107 views
Skip to first unread message

farrukh ahmed

unread,
Aug 12, 2022, 2:01:26 AM8/12/22
to ansible...@googlegroups.com
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"


Vladimir Botka

unread,
Aug 12, 2022, 2:48:53 AM8/12/22
to farrukh ahmed, ansible...@googlegroups.com
On Fri, 12 Aug 2022 11:01:01 +0500
farrukh ahmed <farrukha...@gmail.com> wrote:

> *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.*

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

--
Vladimir Botka

Nico Kadel-Garcia

unread,
Aug 13, 2022, 5:58:23 PM8/13/22
to ansible...@googlegroups.com, farrukh ahmed
pkill is a useful tool, and a dangerous one if other processes may
have matching names. Use it cautiously.

Vladimir Botka

unread,
Aug 13, 2022, 7:40:37 PM8/13/22
to Nico Kadel-Garcia, ansible...@googlegroups.com, farrukh ahmed
On Sat, 13 Aug 2022 17:58:02 -0400
Nico Kadel-Garcia <nka...@gmail.com> wrote:

> On Fri, Aug 12, 2022 at 2:48 AM Vladimir Botka <vbo...@gmail.com> wrote:
> >
> > On Fri, 12 Aug 2022 11:01:01 +0500
> > farrukh ahmed <farrukha...@gmail.com> wrote:
> >
> > > *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.*
> >
> > 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


--
Vladimir Botka

Walter Rowe

unread,
Aug 15, 2022, 8:05:52 AM8/15/22
to Ansible Project
Don't you need to put "RUNNING" in quotes? It is a string, no?

  when: "RUNNING" in job_check.stdout_lines

--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

Felix Fontein

unread,
Aug 15, 2022, 2:41:02 PM8/15/22
to ansible...@googlegroups.com
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


Walter Rowe

unread,
Aug 15, 2022, 3:37:52 PM8/15/22
to Ansible Project
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.

+++
---
- name: testing when
  hosts: localhost
  become: no
  gather_facts: no
  vars:
    my_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"
    running: false
  tasks:
    - set_fact:
        running: true
      when: item | regex_search('RUNNING')
      loop: "{{ my_lines }}"

    - debug: msg="running found"
      when: running
+++

+++
% ansible-playbook -i localhost, foo.yml

PLAY [testing when] ****************************************************************************************************

TASK [set_fact] ********************************************************************************************************
ok: [localhost] => (item=idle-queue:idle-queue_00         RUNNING   pid 32292, uptime 1:21:01)
ok: [localhost] => (item=idle-queue:idle-queue_01         RUNNING   pid 32293, uptime 1:21:01)
ok: [localhost] => (item=idle-queue:idle-queue_02         RUNNING   pid 32291, uptime 1:21:01)

TASK [debug] ***********************************************************************************************************
ok: [localhost] => {
    "msg": "running found"
}

PLAY RECAP *************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

+++

Below I misspell 'RUNING' on purpose to show it works as expected.

+++
---
- name: testing when
  hosts: localhost
  become: no
  gather_facts: no
  vars:
    my_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"
    running: false
  tasks:
    - set_fact:
        running: true
      when: item | regex_search('RUNING')
      loop: "{{ my_lines }}"

    - debug: msg="running found"
      when: running
+++

Note below that the set_fact is skipped with each iteration of the loop because 'RUNING' is not found by the regex_search. When a regex_search does not find what it is looking for it returns the empty string which gets evaluated to false. Any non-empty string will evaluate to true.

+++
% ansible-playbook -i localhost, foo.yml

PLAY [testing when] ****************************************************************************************************

TASK [set_fact] ********************************************************************************************************
skipping: [localhost] => (item=idle-queue:idle-queue_00         RUNNING   pid 32292, uptime 1:21:01) 
skipping: [localhost] => (item=idle-queue:idle-queue_01         RUNNING   pid 32293, uptime 1:21:01) 
skipping: [localhost] => (item=idle-queue:idle-queue_02         RUNNING   pid 32291, uptime 1:21:01) 

TASK [debug] ***********************************************************************************************************
skipping: [localhost]

PLAY RECAP *************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=0    skipped=2    rescued=0    ignored=0
+++

--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

Felix Fontein

unread,
Aug 15, 2022, 3:44:26 PM8/15/22
to ansible...@googlegroups.com
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

Walter Rowe

unread,
Aug 15, 2022, 4:20:02 PM8/15/22
to Ansible Project
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.
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce

jbor...@gmail.com

unread,
Aug 15, 2022, 4:53:03 PM8/15/22
to Ansible Project
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'

Rowe, Walter P. (Fed)

unread,
Aug 16, 2022, 7:19:10 AM8/16/22
to ansible...@googlegroups.com
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:

+++
---
- name: testing when
  hosts: localhost
  become: no
  gather_facts: no
  vars:
    my_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"
  tasks:

    - debug: msg="running found"
      when: '"RUNNING" in my_lines'
+++

yields this output:

+++
% ansible-playbook -i localhost, foo.yml

PLAY [testing when] ****************************************************************************************************

TASK [debug] ***********************************************************************************************************
skipping: [localhost]

PLAY RECAP *************************************************************************************************************
localhost                  : ok=0    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0   
+++

Walter
--
Walter Rowe, Chief
Infrastructure Services
Office of Information Systems Management
National Institute of Standards and Technology
United States Department of Commerce
-- 
You received this message because you are subscribed to a topic in the Google Groups "Ansible Project" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ansible-project/daKrxfpknXo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/88bf67d4-23fb-4b6f-8387-2cba80db70ean%40googlegroups.com.

Rowe, Walter P. (Fed)

unread,
Aug 16, 2022, 7:23:10 AM8/16/22
to ansible...@googlegroups.com
Example:

+++
---
- name: testing when
  hosts: localhost
  become: no
  gather_facts: no
  vars:
    my_lines:
      - "idle-queue:idle-queue_00         RUNNING   pid 32292, uptime 1:21:01"
      - "RUNNING"
      - "idle-queue:idle-queue_01         RUNNING   pid 32293, uptime 1:21:01"
      - "idle-queue:idle-queue_02         RUNNING   pid 32291, uptime 1:21:01"
  tasks:

    - debug: msg="running found"
      when: '"RUNNING" in my_lines'
+++


+++
% ansible-playbook -i localhost, foo.yml

PLAY [testing when] ****************************************************************************************************

TASK [debug] ***********************************************************************************************************
ok: [localhost] => {
    "msg": "running found"
}

PLAY RECAP *************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

Todd Lewis

unread,
Aug 16, 2022, 7:52:06 AM8/16/22
to Ansible Project
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")
On Friday, August 12, 2022 at 2:01:26 AM UTC-4 farrukha...@gmail.com wrote:

Rowe, Walter P. (Fed)

unread,
Aug 16, 2022, 7:58:31 AM8/16/22
to ansible...@googlegroups.com
Yes, that works also, and is simpler than my example. Nice!

+++
---
- name: testing when
  hosts: localhost
  become: no
  gather_facts: no
  vars:
    my_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"
  tasks:

    - debug: msg="running found"
      when: my_lines is search('RUNNING')
+++

... yields ...

farrukh ahmed

unread,
Aug 19, 2022, 4:16:01 AM8/19/22
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages