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
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.
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!"
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With