Access to hostvars from Ansible callback

Hello guys,

I have a question regarding ansible callback_plugins and hostvars in the runner_ok() method.

I can retrieve facts on a specific host from this method by using the ‘res’ argument provided.
However I’d like to collect hostvars too in this method but from my custom CallbackModule I don’t have access to such information, nor do I have access to the runner itself which holds hostvars.

Do you have any trick or idea to be able to retrieve hostvar from a custom callback_plugin runner_ok() method ?

Thanks in advance :slight_smile:

N.B : The first thing I think of is to create a new class that inherits from DefaultRunnerCallbacks in ansible.callback.py module

The big thing to know is that you have access to the play and playbook instances from your callback, such as:

self.playbook and self.play.

As such you have access to things like self.playbook.SETUP_CACHE, self.playbook.VARS_CACHE and self.playbook.inventory

The Runner class has a method called get_inejct_vars.

inject = runner.get_inject_vars(host)

If you specify a host, it will yield a dictionary of variables. I’m not sure, but there might be a key in the inject dictionary that contains host_vars and holds the variables. Hope that helps a bit.

Justin

Thanks for the answer. I forgot to mention that I am using ansible as python API - meaning using directly the ansible Runner class.
I don’t think I will have access to any playbook object in that situation.