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

On Aug 15, 2022, at 4:53 PM, [email protected]<http://gmail.com> 
<[email protected]<mailto:[email protected]>> wrote:

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'

On Tuesday, August 16, 2022 at 6:20:02 AM UTC+10 
[email protected]<http://nist.gov> wrote:
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

On Monday, August 15, 2022 at 3:44:26 PM UTC-4 Felix Fontein wrote:
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

--
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 
[email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/88bf67d4-23fb-4b6f-8387-2cba80db70ean%40googlegroups.com.

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/56C36584-4B52-4883-9AB8-ED8757CF84A4%40nist.gov.

Reply via email to