\e[0m comapre this string in failed_when condition

37 views
Skip to first unread message

Kunalsing Thakur

unread,
Apr 4, 2019, 3:00:45 AM4/4/19
to Ansible Project
Hello Team,
When i was checking for string comparison in failed_when condition.
the ansible task does not give any output if search is not found. but it will print stdout variable as   stdout: "\e[0m"
But when i try to compare my ansible playbook gives error.
I tried two ways in codition:-
1) failed_when: "'{{\e[0m}}' in pulp.results.[0].stdout"
Error which i got:-
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:55:59 +0000 (0:00:02.078)       0:00:06.052 ********
fatal: [pulp]: FAILED! =>
  msg: "The conditional check ''{{\e[0m}}' in pulp.results.[0].stdout' failed. The error was: template error while templating string: unexpected char u'\\x1b' at 3. String: '{{\e[0m}}' in pulp.results.[0].stdout"

2)failed_when: pulp.results.[0].stdout == "{{\e[0m}}"
fatal: [inplp01p2.infra.smf1.mobitv]: FAILED! =>
  msg: |-
    The conditional check 'pulp.results.[0].stdout == "{{\e[0m}}"' failed. The error was: unexpected char u'\\' at 30
      line 1



task:-
---
- 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:
        - mobi-auth-manager-5.42.0-2019h
  serial: 1
  any_errors_fatal: true
  roles:
    - role: mobi_ansible_role_pulp


role:-
---
- 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: "'{{\e[0m}}' in pulp.results.[0].stdout"
- debug:
    var: pulp.results.0.stdout

Kunalsing Thakur

unread,
Apr 4, 2019, 3:24:32 AM4/4/19
to Ansible Project
or is there way we can compare empty string.

Kunalsing Thakur

unread,
Apr 5, 2019, 2:12:03 PM4/5/19
to Ansible Project
Gentle reminder, is anyone can help on this

Matt Martz

unread,
Apr 5, 2019, 3:46:12 PM4/5/19
to ansible...@googlegroups.com
You would need to convert that value from a shell escape sequence, to a python escape sequence, for consumption by PyYAML.

The easiest way to express this will likely be:

- 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: escape in pulp.results.[0].stdout
  vars:
    escape: "\u001b[0m"


On Fri, Apr 5, 2019 at 1:12 PM Kunalsing Thakur <bits.ku...@gmail.com> wrote:
Gentle reminder, is anyone can help on this

--
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/ba24777d-4e0d-4899-9c6a-419b866124a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Matt Martz
@sivel
sivel.net

Kunalsing Thakur

unread,
Apr 6, 2019, 5:58:41 AM4/6/19
to Ansible Project
Hello Matt,

 i tried with task

- name: searching the rpm into {{ reponame }} repo
  shell: pulp-admin rpm repo content rpm --repo-id={{reponame}} --match 'filename={{ item }}'

  with_items: "{{ rpmname | default([]) }}"
  register: pulp
  when: search is defined

  failed_when: escape in pulp.results.[0].stdout
  vars:
   escape: "\u001b[0m"


But get jinja templating error as below:-
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  09:55:41 +0000 (0:00:02.011)       0:00:02.434 ********
fatal: [node]: FAILED! =>
  msg: 'The conditional check ''escape in pulp.results.[0].stdout'' failed. The error was: template error while templating string: expected name or number. String: {% if escape in pulp.results.[0].stdout %} True {% else %} False {% endif %}'

I tried use following syntax also:-

- name: searching the rpm into {{ reponame }} repo
  shell: pulp-admin rpm repo content rpm --repo-id={{reponame}} --match 'filename={{ item }}'

  with_items: "{{ rpmname | default([]) }}"
  register: pulp
  when: search is defined

  failed_when: 'escape in pulp.results.[0].stdout'
  vars:
   escape: "\u001b[0m"

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  09:56:37 +0000 (0:00:02.166)       0:00:02.655 ********
fatal: [node]: FAILED! =>
  msg: 'The conditional check ''escape in pulp.results.[0].stdout'' failed. The error was: template error while templating string: expected name or number. String: {% if escape in pulp.results.[0].stdout %} True {% else %} False {% endif %}'

Any Help will be much appreciated..




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


--

Kunalsing Thakur

unread,
Apr 6, 2019, 6:08:43 AM4/6/19
to Ansible Project
Hello Matt,
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. :)

Reply all
Reply to author
Forward
0 new messages