failed_when not working for empty sting output in task output

playbook:-

Can anyone help on this?

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

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:

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

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’’’

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

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’’’

results only exists if you registered in a loop

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:-

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

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

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

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

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

   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.

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 ?

ansible task not failed still its passing.

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

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. :slight_smile: