Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ansible

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"}
like image 535
Omar E Avatar asked Aug 21 '16 23:08

Omar E


People also ask

What is Failed_when in Ansible?

Ansible lets you define what “failure” means in each task using the failed_when conditional. As with all conditionals in Ansible, lists of multiple failed_when conditions are joined with an implicit and , meaning the task only fails when all conditions are met.

How can Error Handling be done in Ansible?

In Ansible, when a particular command or module returns a zero code, the command is successfully executed without any failure. If there is a non-zero code, which means this is a command failure, it will stop the execution on that particular host and continue on the other host.

What happens when one playbook fails a task?

Handlers and Failure If a task later on in the same play fails, the service will not be restarted despite the configuration change. You can change this behavior with the --force-handlers command-line option, or by including force_handlers: True in a play, or force_handlers = True in ansible.

What is .RC in Ansible?

rc. Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on), this field contains 'return code' of these utilities.


1 Answers

You may help ansible to avoid templating crash because of undefined variable.
Change fail_when: like this:

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

If job is not finished by the first run of polling task, stdout is not yet populated and thus undefined, causing templating engine to crash.

like image 115
Konstantin Suvorov Avatar answered Oct 03 '22 01:10

Konstantin Suvorov