Hi,
In an Ansible V2 callback plugin :
Within def v2_playbook_on_play_start(self, play):
I have the list of hosts to play with in play.hosts (which is fine)
Within def v2_playbook_on_stats(self, stats):
I have the list of hosts played in stats.processed.keys() (which is fine)
Within def v2_runner_on_ok(self, result, **kwargs):
I have the host that was run in result._host (which is fine)
Within def v2_playbook_on_task_start(self, task, **kwargs):
I can’t find out where to get the host that will be run for that particular task (which is bad)
Where do I get that information ?
If nothing is already provided for, will it make sense to add an attribute “host” to the class Task and put in it :
- the host_name (on which the task will be run)
or
- the host object from inventory ?
(whatever sounds best for you)
I’d love to call task.get_host() from v2_playbook_on_task_start(self, task, **kwargs)
Best regards - Eric
You don't, normally on_task_start is called before hosts are iterated over.
I guess it might depend on current (and future) strategies.
But for the time being, an host has always been elected for queuing task before on_task_start is called.
At least for the free strategy, the two followng calls come together :
self._tqm.send_callback('v2_playbook_on_task_start', task, is_conditional=False)
self._queue_task(host, task, task_vars, play_context)
And it could be a good idea (option in /etc/ansible/ansible.cfg ?) to enforce that on_task_start is called before each call to _queue_task() if needed, whatever the strategy used.
If one prefers a single call to “on_task_start” with no host for the linear strategy (whatever the number of hosts launched) no problem, the option will deal with that too.
(by the way, the “patch” to implement the idea is not very difficult to code)
Best regards - Eric
Hi,
As I mentioned, “patch” to implement the idea was not difficult to do. Yet, it should be reviewed (and corrected if needed).
The following files were impacted :
./lib/ansible/config/init.py
./lib/ansible/vars/reserved.py
=> to create an “option” by vars (but does it need to be an option ?)
./lib/ansible/playbook/task.py
./lib/ansible/plugins/strategy/free.py
./lib/ansible/plugins/strategy/linear.py
./lib/ansible/plugins/strategy/init.py
./lib/ansible/plugins/callback/default.py
=> We now have a callback at the start and at the stop of each task for a specified host
whatever the strategy used :
We then should be able to do the following :
- know in real time which tasks are still running, those finished and those yet to come.
- know the hosts in strategy=linear that never end a task (thus preventing the launch of the next task for all hosts)
- suspend execution of a task for a specified host untill all external requirements are done
- change the level of debug for a specific host/task
- etc
Banners (at start of task for a group of hosts) are done as before.
Best regards - Eric