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