# Store playbook name as a 'global' variable across plays?

**URL:** <https://forum.ansible.com/t/store-playbook-name-as-a-global-variable-across-plays/22984>\
**Category:** Ansible Project\
**Created:** [November 8, 2016, 10:59pm UTC](https://forum.ansible.com/t/store-playbook-name-as-a-global-variable-across-plays/22984 "2016-11-08T22:59:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![burns498](https://avatars.discourse-cdn.com/v4/letter/b/b487fb/32.png) [@burns498](https://forum.ansible.com/u/burns498)\
**Post date:** [November 8, 2016, 10:59pm UTC](https://forum.ansible.com/t/store-playbook-name-as-a-global-variable-across-plays/22984/1 "2016-11-08T22:59:30Z")

</div>

Hello -

Is there a way to store playbook name as a ‘global’ variable across plays?? I’ve seen potential options via [callbacks](https://github.com/ansible/ansible/issues/14772) or [group\_vars:all](http://grokbase.com/t/gg/ansible-devel/149s71x0gv/global-variables-file) but still struggling with the solve.

At the end of the day I want to log certain actions for a specific playbook (which contains several different plays). I have the default ansible logging setup, but it lacks readability and is otherwise difficult to consume. The playbook contains all sorts of prep, validations, and deployments but I only want to log the aggregate changes in our operational environments such that it can be easily digested. I envision a log file with \<playbook\_name\>\_.log with all the stuff I want to plop into it.

I could approach the callback; however I’m still new to ansible so hesitating before hopping into more ‘advanced’ features.  
I don’t believe group\_vars:all will work because the playbook name isn’t tied to the host groups.  
Passing in the name as an extra-var on the command line reduces usability.  
So far the best that I could do is manually define the var in the playbook when calling the role. However, it then only exists for that specific play.

`

- name: MHE Dematic Conveyer Configuration  
hosts: “{{ target }}”  
vars:  
playbook\_name: “pb\_MA\_WMOS\_Post\_Install\_Config”  
roles:
- { role: role\_mhe\_config\_lower, when: “‘Lower\_Envs’ in group\_names”, tags: [“Lower\_Envs”] }
- { role: role\_mhe\_config\_upper, when: “‘Upper\_Envs’ in group\_names”, tags: [“Upper\_Envs”] }

`

Or, perhaps I’m just looking at this all wrong and restricting too much down one path. Thoughts?

Thanks!

---

<div class="post-metadata">

**Author:** ![Dick\_Davies](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@Dick\_Davies](https://forum.ansible.com/u/Dick_Davies)\
**Post date:** [November 10, 2016, 12:25am UTC](https://forum.ansible.com/t/store-playbook-name-as-a-global-variable-across-plays/22984/2 "2016-11-10T00:25:31Z")

</div>

you could set it as a group\_var in your inventory.

Do you mind me asking what's going on with your roles there (I don't  
understand what the target var is for)?  
If you have 2 different roles that you want to apply to groups,  
couldn't you just have multiple plays?

...  
...  
- hosts: Lower\_Envs  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- role\_mhe\_config\_lower

- hosts: Upper\_Envs  
&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- role\_mhe\_config\_upper  
...  
...

---

<div class="post-metadata">

**Author:** ![burns498](https://avatars.discourse-cdn.com/v4/letter/b/b487fb/32.png) [@burns498](https://forum.ansible.com/u/burns498)\
**Post date:** [November 28, 2016, 11:23pm UTC](https://forum.ansible.com/t/store-playbook-name-as-a-global-variable-across-plays/22984/3 "2016-11-28T23:23:33Z")

</div>

Hello - Apologies for the late reply. I failed to see the update before.

Thanks for the suggestion. I ended up defining this as a fact early on in my playbook.

As for your question on roles, I do agree with you. I already redesigned the role setup to make more sense. Thanks!
