# failed\_when not working for empty sting output in task output

**URL:** <https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050>\
**Category:** Ansible Project\
**Created:** [April 3, 2019, 1:45pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050 "2019-04-03T13:45:25Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 3, 2019, 1:45pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/1 "2019-04-03T13:45:25Z")

</div>

playbook:-

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 3, 2019, 1:51pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/2 "2019-04-03T13:51:47Z")

</div>

Can anyone help on this?

---

<div class="post-metadata">

**Author:** ![Raghavendra\_Rao](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@Raghavendra\_Rao](https://forum.ansible.com/u/Raghavendra_Rao)\
**Post date:** [April 3, 2019, 2:26pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/3 "2019-04-03T14:26:58Z")

</div>

Hmmm… it should fail… as the string is matched…

What happens when you do this instead?:

- name: searching the rpm into pulp repo  
command: pulp-admin rpm repo content rpm --repo-id=mobi-snapshots --match ‘filename={{ item }}’  
loop: “{{ rpmname | default() }}”  
register: pulpresult  
when: rpmsearch is defined  
ignore\_errors: yes

- name: invoke failure

fail:

when: pulpresult.stdout == “\e[0m”

-R.Rao

---

<div class="post-metadata">

**Author:** ![kunalsing\_thakur1](https://avatars.discourse-cdn.com/v4/letter/k/7feea3/32.png) [@kunalsing\_thakur1](https://forum.ansible.com/u/kunalsing_thakur1)\
**Post date:** [April 3, 2019, 2:32pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/4 "2019-04-03T14:32:32Z")

</div>

TASK [mobi\_ansible\_role\_pulp : invoke failure] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/roles/mobi\_ansible\_role\_pulp/tasks/pulp.yaml:9  
Wednesday 03 April 2019 14:32:02 +0000 (0:00:02.598) 0:00:07.161 \*\*\*\*\*\*\*  
fatal: [node]: FAILED! =\>  
msg: |-  
The conditional check ‘pulpresult.stdout == “\e[0m”’ failed. The error was: error while evaluating conditional (pulpresult.stdout == “\e[0m”): ‘dict object’ has no attribute ‘stdout’

The error appears to have been in ‘/etc/ansible/roles/mobi\_ansible\_role\_pulp/tasks/pulp.yaml’: line 9, column 3, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 3, 2019, 2:33pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/5 "2019-04-03T14:33:15Z")

</div>

Wednesday 03 April 2019 14:32:02 +0000 (0:00:02.598) 0:00:07.161 \*\*\*\*\*\*\*  
fatal: [node]: FAILED! =\>  
msg: |-  
The conditional check ‘pulpresult.stdout == “\e[0m”’ failed. The error was: error while evaluating conditional (pulpresult.stdout == “\e[0m”): ‘dict object’ has no attribute ‘stdout’

The error appears to have been in ‘/etc/ansible/roles/mobi\_ansible\_role\_pulp/tasks/pulp.yaml’: line 9, column 3, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

- name: invoke failure  
^ here

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 3, 2019, 2:42pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/6 "2019-04-03T14:42:06Z")

</div>

Hi Rao,  
when i check with this condition  
failed\_when: pulp.results.0.stdout == “\e[0m”  
it gives me following error

TASK [mobi\_ansible\_role\_pulp : searching the rpm into pulp repo] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/roles/mobi\_ansible\_role\_pulp/tasks/pulp.yaml:4  
Wednesday 03 April 2019 14:40:58 +0000 (0:00:01.856) 0:00:06.231 \*\*\*\*\*\*\*  
fatal: [inplp01p2.infra.smf1.mobitv]: FAILED! =\>  
msg: ‘The conditional check ‘‘pulp.results.0.stdout == “\e[0m”’’ failed. The error was: error while evaluating conditional (pulp.results.0.stdout == “\e[0m”): ‘‘dict object’’ has no attribute ‘‘results’’’

---

<div class="post-metadata">

**Author:** ![Raghavendra\_Rao](https://avatars.discourse-cdn.com/v4/letter/r/c77e96/32.png) [@Raghavendra\_Rao](https://forum.ansible.com/u/Raghavendra_Rao)\
**Post date:** [April 3, 2019, 3:15pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/7 "2019-04-03T15:15:56Z")

</div>

I see you used debug to get a verbose out from that specific command.

Does that list stdout as one of the return values?

-R.Rao

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 3, 2019, 3:25pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/8 "2019-04-03T15:25:41Z")

</div>

Yes ,

pulp.results.0.stdout give me output “\e[0m”

But when I try to access in failed\_when condition  
It is giving me below error.  
fatal: [node]: FAILED! =\>  
msg: ‘The conditional check ‘‘pulp.results.0.stdout == “\e[0m”’’ failed. The error was: error while evaluating conditional (pulp.results.0.stdout == “\e[0m”): ‘‘dict object’’ has no attribute ‘‘results’’’

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 3, 2019, 7:43pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/9 "2019-04-03T19:43:31Z")

</div>

results only exists if you registered in a loop

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 4, 2019, 6:25am UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/10 "2019-04-04T06:25:53Z")

</div>

Hi Brian,  
I have registered results. i am able to access the content using pulp.results.[0].stdout. if nothing is found stdout prints this “\e[0m”. And i want to compare this string to failed the task using failed\_when condition.  
but somehow it gives me below error.  
fatal: [pulp]: FAILED! =\>  
msg: ‘The conditional check ‘‘pulp.results.[0].stdout == “\e[0m”’’ failed. The error was: template error while templating string: expected name or number. String: {% if pulp.results.[0].stdout == “\e[0m” %} True {% else %} False {% endif %}’

Playbook:-

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 4, 2019, 6:38am UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/11 "2019-04-04T06:38:02Z")

</div>

is there any example which i refer like this to get sorted out?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 4, 2019, 7:36am UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/12 "2019-04-04T07:36:29Z")

</div>

Sebastian Meyer gave you the answer to this in you other thread, maybe read that one more carefully?

[https://groups.google.com/d/msgid/ansible-project/0749a574-c80b-a765-72c9-928842320e95%40b1-systems.de](https://groups.google.com/d/msgid/ansible-project/0749a574-c80b-a765-72c9-928842320e95%40b1-systems.de)

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 4, 2019, 7:40am UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/13 "2019-04-04T07:40:27Z")

</div>

can you tell me what should be written in role? i am trying to understand but can’t intepret in role.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 4, 2019, 7:47am UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/14 "2019-04-04T07:47:57Z")

</div>

- name: Logging into pulp  
shell: pulp-admin login -u admin -p admin

- name: searching the rpm into {{ reponame }} repo  
shell: pulp-admin rpm repo content rpm --repo-id={{reponame}} --match ‘filename={{ item }}’  
loop: “{{ rpmname | default() }}”  
register: pulp  
when: search is defined  
failed\_when: pulp.stdout == “\e[0m”

Also i tried to use this one.

But ansible task will pass without fail.

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [April 4, 2019, 8:08am UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/15 "2019-04-04T08:08:31Z")

</div>

This is the correct syntax.

The reason it doesn't work can be many thing and the trial and error might be you best option.

Your backslash in failed\_when is probably taken as an escape.  
- So try double backslash to escape the the backslash.  
- Try changing to single quotes instead of double(with and without double backslashes)

Alternative is to just check for partial match(leave out the backslash).

&nbsp;&nbsp;&nbsp;failed\_when: "'e[0m' in pulp.stdout"

If that will not work you have a lot of filter that can do regexp that you can look into.

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 4, 2019, 8:50am UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/16 "2019-04-04T08:50:39Z")

</div>

Hi kai,  
thanks for information.  
i tried below condition  
failed\_when: pulp.stdout == “\e[0m”  
failed\_when: pulp.stdout == ‘\e[0m’  
failed\_when: pulp.stdout == “\e[0m”  
failed\_when: pulp.stdout == “\e[0m”  
failed\_when: “‘e[0m’ in pulp.stdout”

i tried all stuff but no luck

any new suggestion ?

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 4, 2019, 8:51am UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/17 "2019-04-04T08:51:07Z")

</div>

ansible task not failed still its passing.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [April 4, 2019, 7:39pm UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/18 "2019-04-04T19:39:10Z")

</div>

''pulp.results.[0].std" is incorrect you have extra . “pullp.results[0].std” is the correct form

---

<div class="post-metadata">

**Author:** ![Kunalsing\_Thakur](https://avatars.discourse-cdn.com/v4/letter/k/3d9bf3/32.png) [@Kunalsing\_Thakur](https://forum.ansible.com/u/Kunalsing_Thakur)\
**Post date:** [April 6, 2019, 10:08am UTC](https://forum.ansible.com/t/failed-when-not-working-for-empty-sting-output-in-task-output/30050/19 "2019-04-06T10:08:09Z")

</div>

Hi Brian,

I was able to successfully do this task by following the instructions.  
Thanks for matt to give some logic.

- name: searching the rpm into {{ reponame }} repo  
shell: pulp-admin rpm repo content rpm --repo-id={{reponame}} --match ‘filename={{ item }}’  
loop: “{{ rpmname | default() }}”  
register: pulp  
when: search is defined  
failed\_when: pulp.stdout == “\u001b[0m”

Result:-  
TASK [mobi\_ansible\_role\_pulp : searching the rpm into infra] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
task path: /etc/ansible/roles/mobi\_ansible\_role\_pulp/tasks/pulp.yaml:5  
Saturday 06 April 2019 10:07:15 +0000 (0:00:02.298) 0:00:02.709 \*\*\*\*\*\*\*\*  
failed: [node] (item=GoIP-devel-1.6.11-201711302323.798cb6f.el7.centos.x86\_64.rpm) =\> changed=true  
cmd: pulp-admin rpm repo content rpm --repo-id=mobi-infra.smf1.mobitv --match ‘filename=GoIP-devel-1.6.11-201711302323.798cb6f.el7.centos.x86\_64.rpm’  
delta: ‘0:00:00.781775’  
end: ‘2019-04-06 10:07:17.178794’  
failed\_when\_result: true  
item: GoIP-devel-1.6.11-201711302323.798cb6f.el7.centos.x86\_64.rpm  
rc: 0  
start: ‘2019-04-06 10:07:16.397019’  
stderr: ‘’  
stderr\_lines:   
stdout: “\e[0m”  
stdout\_lines:

This issue is resolved now. 🙂
