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

On Monday, August 15, 2022 at 2:41:02 PM UTC-4 Felix Fontein wrote:

> 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 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/6902433e-4db7-4e41-bd7d-c1783f55cf7cn%40googlegroups.com.

Reply via email to