Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check stdout of async Ansible task

Tags:

ansible

How can you failed_when based on the stdout of an async Ansible task? I've tried variations of:

- name: Run command
  command: arbitrary_command
  async: 3600
  poll: 10
  register: result
  failed_when: "Finished 'command'" in result.stdout

This results in:

fatal: [localhost] => error while evaluating conditional: "Finished 'command'" in result.stdout
like image 865
Fraser Harris Avatar asked May 03 '16 09:05

Fraser Harris


People also ask

How do you show output in Ansible?

To capture the output, you need to specify your own variable into which the output will be saved. To achieve this, we use the 'register' parameter to record the output to a variable. Then use the 'debug' module to display the variable's content to standard out.

What is stdout in Ansible?

stdout. Some modules execute command line utilities or are geared for executing commands directly (raw, shell, command, and so on). This field contains the normal output of these utilities. "stdout": "foo!"

What is asynchronous polling in Ansible?

If you want to run multiple tasks in a playbook concurrently, use async with poll set to 0. When you set poll: 0 , Ansible starts the task and immediately moves on to the next task without waiting for a result. Each async task runs until it either completes, fails or times out (runs longer than its async value).

What does poll do in Ansible?

Ansible Poll Keyword with Ansible async The poll keyword accepts a numeric value to know how many seconds it should wait before polling or following up with the long-running asynchronous task. In other words, The value of the poll indicates how often to poll and check if the tasks are completed.


1 Answers

Obtain status of asynchronous task with async_status once the task is complete:

- name: Run command
  command: arbitrary_command
  async: 3600
  poll: 10
  register: result_async

- name: Check command
  async_status: jid="{{ result_async.ansible_job_id }}"
  register: result
  failed_when: result.finished != 1 or "Finished 'command'" not in result.stdout
like image 188
Fraser Harris Avatar answered Oct 26 '22 09:10

Fraser Harris