Ansible: execute fail_when: in an async job based on the value from stdout

I am trying to execute failed_when:for an asynchronous task based on the 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

Here I check the status of the task and want it not to be executed 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 launch the above play, I encounter 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"}
+6
source share
1 answer

You can help avoid problems with template failures due to the undefined variable.
Change fail_when:as follows:

failed_when: "poll_status.stdout is defined and 'ERROR' in poll_status.stdout"

, stdout , , undefined, .

+8

Source: https://habr.com/ru/post/1651968/


All Articles