This repository provides a minimal demonstration of running an ansible playbook and seeing the output of a shell command in real time (as the command is running).
The playbook includes the role long_run that executes the task asynchronously and calls the role watch that watches the job and shows its output incrementally, waiting a specified amount of seconds (long_run_poll) between consecutive displays, giving a near real time behaviour (for small values of long_run_poll).
- If the job is ended with an error code, the watch task also ends with an error.
- If the job had no output in some (or all) iterations of the watch job, there will be no output displayed by ansible in such iterations.
- It was created a custom callback plugin to allow printing ansible tasks with the
okstatus only if the task has a tagprint_action(along with the optionsdisplay_ok_hosts = noanddisplay_skipped_hosts = noinansible.cfg) to print only relevant stuff. - The time between 2 consecutive outputs from the watch job is the time specified in
long_run_pollplus the time taken to execute the other tasks to update the state of the job, retrieving the lines, actually displaying it, and so on.
To make it easier to run the playbook, it was created a docker compose file to allow running the playbook inside a docker container with Ansible 2.8 installed (the image is already provided, you can simply run the commands below).
(You need to have docker and docker-compose installed to run it with docker, but if you have Ansible 2.8 you should be able to run the step 2 directly, maybe needing to fix some permisson issues or change the directories paths in main.yml to avoid such issues).
docker-compose run --rm ansible /bin/bashansible-playbook main.yml-
Change
long_run_pollinmain.ymlfrom3to1and see that the output is show in shorter intervals. -
Change
files/task.shincluding some kind of error in the script to see a case in which the task is executed unsuccessfully. -
Remove the strings in the array of
files/task.shto see a case in which there is no output from the task. -
Change the file
ansible.cfgcommenting the linesdisplay_ok_hostsandstdout_callbackto see that many unnecessary stuff is displayed. It can be much worse for tasks that may take a considerable amount of time with a small output (that's the main reason for using the custom callback plugin). -
To make it work in other projects, include the roles
long_runandwatchas well as the callback plugin, and enable the plugin in theansible.cfgfile, defining alsodisplay_ok_hosts = noanddisplay_skipped_hosts = no. Keep in mind that the tasks in the project that ends with theokstatus will not be shown, but you can make they show including a tagprint_actionin the task. You can use this repository as a reference.
