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

Hello -

Is there a way to store playbook name as a ‘global’ variable across plays?? I’ve seen potential options via callbacks or group_vars:all 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!

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
  roles:
    - role_mhe_config_lower

- hosts: Upper_Envs
  roles:
    - role_mhe_config_upper
...
...

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!