Reputation: 3793
I am playing with Ansible callbacks and would like to use the name (for example "ansible-test") of the playbook being executed within the v2_playbook_on_start(self, playbook)
method.
So far, here is how I code my custom callback
class CallbackModule(CallbackBase):
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'notification'
CALLBACK_NAME = 'XXXX'
CALLBACK_NEEDS_WHITELIST = True
def __init__(self, display=None):
super(CallbackModule, self).__init__(display=display)
def v2_playbook_on_start(self, playbook):
# How to get the playbook name?
def v2_playbook_on_stats(self, stats):
# ...
I already tried several things, but nothing works so far:
playbook._load_playbook_data
, playbook.__module__
And I can't find anything in the docs.
How can I get this name?
Note: In my case, I can't use playbook._basedir
EDIT
Some more details to clarify my point.
So far, my structure is like the following:
- ansible-deploy-apache
- defaults
- main.yml
- tasks
- main.yml
- vars
- ...
Here, the tasks the playbook execute are defined in tasks/main.yml
.
What playbook._file_name
gives me is main.yml
(not the content, but just the name) from tasks
. What I would like to have from the callback method ansible-deploy-apache
is instead ansible-deploy-apache
.
Upvotes: 2
Views: 2854
Reputation: 81
This code publishes a fact for all hosts :
def v2_playbook_on_start(self,playbook):
self.env = {
"playbook_name": playbook._file_name
}
def v2_playbook_on_play_start(self, play):
variable_manager = play._variable_manager
hosts = variable_manager._inventory.get_hosts()
for host in hosts:
variable_manager.set_host_variable(host,"env",self.env)
Then in your playbook, you can simply access the fact with :
{{ env.playbook_name }}
Of course you can rename the fact as you want.
Upvotes: 1
Reputation: 68279
You may want to try _file_name
:
def v2_playbook_on_start(self, playbook):
display.warning('Current playbook: {}'.format(playbook._file_name))
Writing plugins require some reverse engineering :)
Context for v2_playbook_on_start
.
Upvotes: 3