Perform a failed_when: on an async task based on a value from stdout

I am trying to perform a failed_when: on an async task based on a value from stdout.

This is my task:

  • name: RUN SOME TASK LOCALLY
    command: <run_some_script_here>.sh
    chdir=/task_folder/
    delegate_to: localhost
    register: task_status
    async: 3600
    poll: 0

This is where I check on the task status and would like it to fail when the specified text is in stdout.

  • name: CHECK TASK PROGRESS
    async_status: jid={{ task_status.ansible_job_id }}
    register: poll_status
    until: poll_status.finished
    retries: 100
    failed_when: “‘ERROR. TASK FAILED.’ in poll_status.stdout”

When I run the above playbook I am faced with the following error from Ansible

TASK [CHECK TASK PROGRESS] *************************************************
fatal: [localhost]: FAILED! => {“failed”: true, “msg”: “The conditional check ‘‘ERROR. TASK FAILED.’ in poll_status.stdout’ failed. The error was: error while evaluating conditional (‘ERROR. TASK FAILED.’ in poll_status.stdout): Unable to look up a name or access an attribute in template string ({% if ‘ERROR. TASK FAILED.’ in poll_status.stdout %} True {% else %} False {% endif %}).\nMake sure your variable name does not contain invalid characters like ‘-’: argument of type ‘StrictUndefined’ is not iterable”}

Help! Thanks in advance.

This communication contains information which is confidential and the copyright of Whispir or a third party. If you have received this email in error please notify us by return email or telephone Whispir on +613 8630 9900 and delete the document and delete all copies immediately. If you are the intended recipient of this communication you should not copy, disclose or distribute this communication without the authority of Whispir. Any views expressed in this Communication are those of the individual sender, except where the sender specifically states them to be the views of Whispir. Except as required at law, Whispir does not represent, warrant and/or guarantee that the integrity of this communication has been maintained nor that the communication is free of errors,virus, interception or interference.

it’s been a while since you posted, but maybe this will help someone else. I have a similar issue - it looks like failed_when just doesn’t work with async tasks. I’ve had to ignore errors on the async task and use ansible’s fail module with a when condition as the next task to fail the play