Hi everyone,
I’m writing several callbacks and modules for my ansible set and I’m wondering if there is a preexisting/simple way to use ansible facts given by setup module in a following callback (different from the first one).
In my case, I have a callback in which I want to use the hostname and the architecture of the node.
In my playbook, setup runs well, a first callback is called and then my playbook continues with a second (custom) module.
After this second module execution I would use the ansible facts provided by the setup module and the only way I found to do this is to write facts in a file when the first callback is executed and then open and read this file in the second callback.
Thanks for reading.
Here is a little diagram that I just drew to make you see how I want it to work (and maybe that you’ll say “OMG, there are so many better ways to do that!” ).
My problem is between the two callback executions in providing ansible setup facts in th second execution.
(attachments)
I'm going to be improving callback "context" very shortly so you'll be
able to get at a very wide variety of things.
Presently there are some limitations about what sort of objects you get handed.
We'll be able to get at a 'context' object which will contain the
runner and playbook objects, and you'll be able to get at hostvars
from there to pull out what you want.
Ok, that’s verry cool! So I’ll wait for that.
<hope=on>Do you think that it will be available in the next version?
Thanks for your awsome work!
2013/3/7 Michael DeHaan <michael.dehaan@gmail.com>
Ok, that's verry cool! So I'll wait for that.
<hope=on>Do you think that it will be available in the next version?</hope>
It's very very very likely. We are going to need it for some
features in ansible-commander (REST layer/UI).
Thanks for your awsome work!
You are welcome!
Hi,
I just installed Ansible 1.1 and I am currently discovering the (amazing) new features.
I still need to use setup infos in my custom “dpkg” playbook and I am trying to understand the new mecanism which allows us to do it.
Unfortunately, I don’t manage to get those informations (hostname, architecture…) in my playbook.
I already tried to use getattr(self, ‘playbook’, None) in my callback (where self = <ansible.callback_plugins.callback_dpkg_tom.CallbackModule object>) but it gives me a object.
I also don’t really understand from where I can take self.playbook, self.play etc…
Could you please help me understand that?
For now, I added this string at the end of the result of my “dpkg” custom module : “changed”: “False”, “invocation2”: {“host_ip”: “192.168.93.37”, “hostname”: “eddv-fnpf-front1-ansible”, “module_args”: “”, “module_name”: “dpkg_tom”}}
and I’m getting it back with this :
8< --------------------------------------------------------------
invocation2 = data.pop(‘invocation2’, None)
if invocation2 is None :
return
else:
data = json.dumps(invocation2)
if invocation2.get(‘module_name’, None) != ‘dpkg_tom’:
return
8< --------------------------------------------------------------
but it is kind of a realy bad hack I think…
Have you got any idea to do it properly?
Best regards,
Tom
So if I understand, you are writing a custom callback and want to
respond to your module named 'dpkg_tom' differently. Got that.
The actual module used per task is not recorded in self.play.task
because that's more about the task object. I can see us upgrading
that.
The invocation hash should be fine to use. There is nothing in
ansible that stores a hash entry called 'invocation2'
Yes. I have :
-
a custom module called “dpkg_tom” which returns the liste of pakages installed (with version etc.) in JSON
-
a callback “callback_setup_inventory.py” which runs only after to have received the “setup” module result
-
another callback “callback_dpkg_tom.py” which runs only after to have received the “dpkg_tom” module result
My final goal is to gather packages and hosts informations to put those informations into a database.
The execution of the “setup” module (and then of “callback_setup_inventory.py”) put the informations about the host into a table of my database.
The execution of the “dpkg_tom” module (and then of “callback_dpkg_tom.py”) put the informations about packages related to the host into another table of my database.
So, in “callback_dpkg_tom.py” I have to know some host informations (given by the “setup” module) like hostname, architecture etc.
I don’t know why but I just can’t use “invocation” in “callback_dpkg_tom.py” (wherease I do in “callback_setup_inventory.py”) so my workaround was to add an “invocation2” JSON hash appended to the “dpkg_tom” module so I’m able to retrieve thos host informations.
Hope that is clearer.
If it’s not… well… I’ll take some english course =P
Tom
I don’t like your idea about adding an ‘invocation2’ but unfortuantely do not have bandwidth to help troubleshoot custom callback plugin Python code.
Someone else may.