failed_when not working for empty sting output in task output

64 views
Skip to first unread message

Kunalsing Thakur

unread,
Apr 3, 2019, 9:45:25 AM4/3/19
to Ansible Project
playbook:-
---
- name: Deploying PCC-test
  hosts: inplp01p2.infra.smf1.mobitv
  gather_facts: true
  vars_files:
    - /etc/ansible/inventories/mobitv/paytv.smf1.mobitv/group_vars/common.yaml
  vars:
    - rpmsearch: true
    - rpmname:
        - kunal
  serial: 1
  any_errors_fatal: true
  roles:
    - role: mobi_ansible_role_pulp



role task:-
pulp.yaml
---
- name: Logging into pulp
  command: pulp-admin login -u admin -p admin
- 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
  failed_when: pulpresult.stdout == "\e[0m"


Ansible Output:-
PLAY [Deploying PCC-test] ****************************************************************************************

TASK [Gathering Facts] *******************************************************************************************
task path: /etc/ansible/playbooks/paytv/pulp.yaml:2
Wednesday 03 April 2019  13:44:13 +0000 (0:00:00.138)       0:00:00.138 *******
ok: [inplp01p2.infra.smf1.mobitv]
META: ran handlers

TASK [mobi_ansible_role_pulp : include_tasks] ********************************************************************
task path: /etc/ansible/roles/mobi_ansible_role_pulp/tasks/main.yml:12
Wednesday 03 April 2019  13:44:17 +0000 (0:00:04.113)       0:00:04.252 *******
included: /etc/ansible/roles/mobi_ansible_role_pulp/tasks/pulp.yaml for inplp01p2.infra.smf1.mobitv

TASK [mobi_ansible_role_pulp : Logging into pulp] ****************************************************************
task path: /etc/ansible/roles/mobi_ansible_role_pulp/tasks/pulp.yaml:2
Wednesday 03 April 2019  13:44:18 +0000 (0:00:00.452)       0:00:04.705 *******
changed: [pulpnode] => changed=true
  cmd:
  - pulp-admin
  - login
  - -u
  - admin
  - -p
  - admin
  delta: '0:00:01.103429'
  end: '2019-04-03 13:44:20.453693'
  rc: 0
  start: '2019-04-03 13:44:19.350264'
  stderr: ''
  stderr_lines: []
  stdout: |-
    [0m[92mSuccessfully logged in. Session certificate will expire at Apr 10 13:44:20 2019
    GMT.[0m
  stdout_lines: <omitted>

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  13:44:20 +0000 (0:00:02.181)       0:00:06.887 *******
changed: [pulpnode] => (item=kunal) => changed=true
  cmd:
  - pulp-admin
  - rpm
  - repo
  - content
  - rpm
  - --repo-id=mobi-snapshots
  - --match
  - filename=kunal
  delta: '0:00:01.359062'
  end: '2019-04-03 13:44:22.679585'
  failed_when_result: false
  item: kunal
  rc: 0
  start: '2019-04-03 13:44:21.320523'
  stderr: ''
  stderr_lines: []
  stdout: "\e[0m"
  stdout_lines: <omitted>
META: ran handlers
META: ran handlers

PLAY RECAP *******************************************************************************************************
inplp01p2.infra.smf1.mobitv : ok=4    changed=2    unreachable=0    failed=0

Wednesday 03 April 2019  13:44:22 +0000 (0:00:02.150)       0:00:09.037 *******
===============================================================================
Gathering Facts ------------------------------------------------------------------------------------------- 4.11s
/etc/ansible/playbooks/paytv/pulp.yaml:2 ------------------------------------------------------------------------
mobi_ansible_role_pulp : Logging into pulp ---------------------------------------------------------------- 2.18s
/etc/ansible/roles/mobi_ansible_role_pulp/tasks/pulp.yaml:2 -----------------------------------------------------
mobi_ansible_role_pulp : searching the rpm into pulp repo ------------------------------------------------- 2.15s
/etc/ansible/roles/mobi_ansible_role_pulp/tasks/pulp.yaml:4 -----------------------------------------------------
mobi_ansible_role_pulp : include_tasks -------------------------------------------------------------------- 0.45s
/etc/ansible/roles/mobi_ansible_role_pulp/tasks/main.yml:12 -----------------------------------------------------
Playbook run took 0 days, 0 hours, 0 minutes, 9 seconds




It should fail the task not pass?

Kunalsing Thakur

unread,
Apr 3, 2019, 9:51:47 AM4/3/19
to Ansible Project
Can anyone help on this?

Raghavendra Rao

unread,
Apr 3, 2019, 10:27:21 AM4/3/19
to ansible...@googlegroups.com
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

--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/070b0841-54b9-4883-b605-0686a135d127%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kunalsing thakur

unread,
Apr 3, 2019, 10:33:02 AM4/3/19
to ansible...@googlegroups.com
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:


For more options, visit https://groups.google.com/d/optout.


--

Kunalsing Thakur

unread,
Apr 3, 2019, 10:33:15 AM4/3/19
to Ansible Project
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



On Wednesday, April 3, 2019 at 7:57:21 PM UTC+5:30, Raghavendra Rao wrote:
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

To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Kunalsing Thakur

unread,
Apr 3, 2019, 10:42:06 AM4/3/19
to Ansible Project
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'''



On Wednesday, April 3, 2019 at 7:57:21 PM UTC+5:30, Raghavendra Rao wrote:
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

To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Raghavendra Rao

unread,
Apr 3, 2019, 11:16:22 AM4/3/19
to ansible...@googlegroups.com
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

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

To post to this group, send email to ansible...@googlegroups.com.

Kunalsing Thakur

unread,
Apr 3, 2019, 11:26:15 AM4/3/19
to Ansible Project
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'''

Brian Coca

unread,
Apr 3, 2019, 3:43:54 PM4/3/19
to Ansible Project
results only exists if you registered in a loop

--
----------
Brian Coca

Kunalsing Thakur

unread,
Apr 4, 2019, 2:26:19 AM4/4/19
to Ansible Project
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:-
---
- name: Deploying PCC-test
  hosts: pulp
  gather_facts: true
  vars_files:
    - /etc/ansible/inventories/mobitv/group_vars/common.yaml

  vars:
    - rpmsearch: true
    - rpmname:
        - mobi-auth-manager-5.42.0-2019h

  serial: 1
  any_errors_fatal: true
  roles:
    - role: mobi_ansible_role_pulp

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

- name: searching the rpm into pulp repo
  shell: pulp-admin rpm repo content rpm --repo-id=mobi-snapshots --match 'filename={{ item }}'
  with_items: "{{ rpmname | default([]) }}"
  register: pulp
  when: rpmsearch is defined
  failed_when: pulp.results.[0].stdout == "\e[0m"
- debug:
    var: pulp.results.0.stdout

Results:-
PLAY [Deploying PCC-test] ***************************************************************************************

TASK [Gathering Facts] ******************************************************************************************
task path: /etc/ansible/playbooks/paytv/pulp.yaml:2
Thursday 04 April 2019  06:24:44 +0000 (0:00:00.139)       0:00:00.139 ********
ok: [pulp]

META: ran handlers

TASK [mobi_ansible_role_pulp : include_tasks] *******************************************************************
task path: /etc/ansible/roles/mobi_ansible_role_pulp/tasks/main.yml:12
Thursday 04 April 2019  06:24:48 +0000 (0:00:03.441)       0:00:03.580 ********
included: /etc/ansible/roles/mobi_ansible_role_pulp/tasks/pulp.yaml for pulp


TASK [mobi_ansible_role_pulp : Logging into pulp] ***************************************************************
task path: /etc/ansible/roles/mobi_ansible_role_pulp/tasks/pulp.yaml:2
Thursday 04 April 2019  06:24:48 +0000 (0:00:00.485)       0:00:04.066 ********
changed: [pulp] => changed=true
  cmd: pulp-admin login -u admin -p admin
  delta: '0:00:01.067098'
  end: '2019-04-04 06:24:50.812519'
  rc: 0
  start: '2019-04-04 06:24:49.745421'
  stderr: ''
  stderr_lines: []
  stdout: |-
    [0m[92mSuccessfully logged in. Session certificate will expire at Apr 11 06:24:50 2019

    GMT.[0m
  stdout_lines: <omitted>

TASK [mobi_ansible_role_pulp : searching the rpm into pulp repo] ************************************************
task path: /etc/ansible/roles/mobi_ansible_role_pulp/tasks/pulp.yaml:4
Thursday 04 April 2019  06:24:50 +0000 (0:00:02.175)       0:00:06.241 ********
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 %}'

NO MORE HOSTS LEFT **********************************************************************************************
        to retry, use: --limit @/etc/ansible/playbooks/paytv/pulp.retry

PLAY RECAP ******************************************************************************************************
pulp : ok=3    changed=1    unreachable=0    failed=1



Any help will be much appreciated.


--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.


--
 

Kunalsing Thakur

unread,
Apr 4, 2019, 2:38:30 AM4/4/19
to Ansible Project
is there any example which i refer like this to get sorted out?

Kai Stian Olstad

unread,
Apr 4, 2019, 3:36:40 AM4/4/19
to ansible...@googlegroups.com
On 04.04.2019 08:25, Kunalsing Thakur wrote:
> 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 %}'

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


--
Kai Stian Olstad

Kunalsing Thakur

unread,
Apr 4, 2019, 3:40:48 AM4/4/19
to Ansible Project
can you tell me what should be written in role? i am trying to understand but can't intepret in role.


--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Kunalsing Thakur

unread,
Apr 4, 2019, 3:48:17 AM4/4/19
to Ansible Project
- 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.


Kai Stian Olstad

unread,
Apr 4, 2019, 4:08:40 AM4/4/19
to ansible...@googlegroups.com
On 04.04.2019 09:47, Kunalsing Thakur wrote:
> - 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.


--
Kai Stian Olstad

Kunalsing Thakur

unread,
Apr 4, 2019, 4:51:03 AM4/4/19
to Ansible Project
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 ?


--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Kunalsing Thakur

unread,
Apr 4, 2019, 4:51:29 AM4/4/19
to Ansible Project
ansible task not failed still its passing.

Brian Coca

unread,
Apr 4, 2019, 3:39:37 PM4/4/19
to Ansible Project
''pulp.results.[0].std" is incorrect you have extra . "pullp.results[0].std" is the correct form

-- 
----------
Brian Coca

Kunalsing Thakur

unread,
Apr 6, 2019, 6:08:35 AM4/6/19
to Ansible Project
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: <omitted>


This issue is resolved now. :)


--
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 ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages