is there a way how I can access host/group vars from within a callback plugin? Basically I want to modify the Hipchat callback plugin to use a room ID configured via group_vars (or maybe registered as a var right in a roles tasks), as we have hundred of chat rooms and to which room a message will be sent would depend on the role.
I can see something like this being set in Runner callbacks, but it may be more appropriate for the “on_stats” method (which is really the final callback when everything is all done) to get passed a reference to the per-host variable structure (internally referred to as the “setup_cache”).
However, the easiest thing you could do, if possible, is just have your callback be responsive to an environment variable like ROOM_ID. However, this would require launching your playbook appropriately.
I’d be interested in hearing more about the use case as having 100 different chatrooms all running playbooks seems like something I’d be interested in
Since I submitted the hipchat.py calback plugin PR, we have since modified it locally, to do something similar to what you are doing.
In our case, our hipchat API token lives in a vault encrypted vars file for all hosts, so we pull this var out during run time. You can see the results at:
I am trying to create vars_plugins plugin and I would need variables passed from cmdline.
Is there any way I could access other vars than host_vars? I would like to pass on cmdline for example -e customer_name=demo and use this variable in my custom vars_plugin and obtain variables specific only for this user…
I have not yet looked into vars plugins. But in theory, if there is the runner object available, you should be able to access those. Try to access self.runner. If it is available the extra vars should be somewhere in there.
Another approach might be, to just dynamically include a yml file, if that’s possible in your case.
I am afraid I will have to go with system envs as mentioned by Michael.
I am currently using dynamic include_vars, but my evil plan counts with having this config in etcd db. I wanna use watcher which will trigger appropriate deploys in case of config change.
vars_plugins are mostly just a way to implement inventory (at a point in time, anyway), and were not really meant to be user serviceable.
Not quite following this part: “and I would need variables passed from cmdline.”
It sounds like you are trying to generate variables from an external source like a CMDB or cloud, and might want to read an environment variable.
if you are doing this, an inventory plugin (if this source already contains the list of hosts/groups/etc as well) would be a good place to return variables at the same time and is usually how we do that.
I am not using inventory plugin because it wouldn’t make sense in my case. Please correct me if it does.
I have dynamic ec2 inventory after I provision CoreOS hosts.
I am running apps in docker containers on top of CoreOS cluster so there can be more or less apps than actual hosts and I mostly don’t specify on which host the app is running.
The apps (fleet unit files) are configured and started using action on local host using fleet module to interact with coreos cluster.
I have general configuration which could be in group_vars/all so that this info is accessible from each host.
Variables are different for each environment, thats why I am loading these values dynamically during run and don’t have those in single static file.
Currently my variables are read first from general config and after overwritten (if present) by customer specific config and after by environment specific config.
When I think about it now I could perhaps convert vars script to dynamic inventory and add variables to local host this way. I believe this should be the only host that I am running those commands from.
My problem with passing variables from cmdline is that if I run ansible-paybook with -e “customer_name=demo customer_env=dev” I would like to import variables for demo customer’s dev environmnent but unfortunatelly I can’t access this variable in my vars_plugin custom python script…
Will I have this variable if I try to implement dynamic inventory?
If it doesn’t bother you to have an extra task, you could do it with an action plugin. At least I know you have access to the runner there. But actually it wouldn’t matter as you could simply pass it in the task like so:
- load_customer_vars: {{ customer_name }}
In the set_facts or include_vars plugins you could see how to return facts from an action plugin:
where you reference self.play, but I can’t figure out where self.play is ever created. The play object is passed in v2_playbook_on_play_start, but that does not have a playbook attribute.