Access All Variables in Plugin - Ansible 2.1

Hi, everyone.

I have an Ansible project that has a number of roles, along with several different playbooks. Several roles require certain variables to be set in order for the roles to execute properly. These variables may be set in a number of different places, such as command line (extra vars), inventory files, other roles, or in playbook vars.

Some of our playbooks can take hours to execute fully, so we would like a way to fail fast if required variables are not set. My initial thought was to check that requirements are met in a callback plugin, with the validation occurring in ‘v2_playbook_on_start’.

When a playbook starts, is there one place where I could find all variables that have been defined? I would like to get something of a “master list” of defined variables for the playbook run, compare it to my list of required variables, and determine whether any required variables are not set. For the master list, I am thinking of something like you would get with {{ vars }} in a debug. However, after looking online and looking at the code, I do not believe such a master list exists - I am hoping someone could tell me otherwise or confirm that the only way to get all variables is to cycle through host vars, play vars, role vars, etc. individually.

Any help or feedback would be very much appreciated!

Thanks,
Nick

short answer: No, except in very restricted cases
long answer:

The way you can define vars, the when, where and scope in which they are available, makes it difficult to know ahead of time if all the vars you need are there as they can be defined dynamically at any point.

You CAN verify the ones you have at any point in the play, that does not mean an include_vars/register/role defaults/set_fact/etc will not define them later.
Your master is hostvars, in which you need to cycle by the hosts that run in each of your plays and dump the vars there and check against that list.

I recommend you add an assert in each role that checks if the variables you need for that role are defined or not at the start, as it is self contained you can predict your external needs at that point, anything else will be expensive to run and fragile.

Hi, Brian.

Thank you very much for the quick reply. Your description makes sense, and I’ll go down the path you recommended.

Thanks,
Nick