Ansible Module to look up variables

I would like to create an Ansible Module that can be used to lookup variables of a given role.

Background:
Our build Git repository is organized in away that we have one Ansible role per micro service (built repo). Those roles have the “default/main.yml”, through which we define a number of default values for the role itself and the variables we override for the “dependencies” roles.

For example, numerious roles have the variable “myrole.python.version”, which typically holds a reference to the respective global version variable that lives in our global variables file. Note, this variable file is available during runtime by the use of the “vars_files” module directive in the initial playbook. So typically, such variable in “default/main.yml” would look like.

myrole:
python:
version: “{{ our.global.vars.python.version }}”

This all works fine when we execute a playbook to build a particular service as it can interpolate those values.

Note, although the majority of those version values are referenced as outline above, some of them are hard coded by assigning a string value (i.e. “1.2.3”) instead of the global variable reference.

Use Case:
In our particular use case, we would like to implement version auditing so we have a complete overview on which versions are used across all our micro services. In order to do this reliably, we need to lookup and interpolate the variables in the same way as a playbook play would do.

Questions:
How would you implement this?

We thought of creating an Ansible module that plays a give playbook programmatically with the “–check” option, then somehow capture a given variable and return it. There are quite a few unknowns for us as to how we can achieve this with such approach. In addition it would require quite a bit of computing to produce one variable value as output. What’s your opinion?

Is there a way of interpolating variables, exactly the way Ansible does when playing a playbook, without actually playing an entire playbook?

Your help is much appreciated!

Check mode might be able to do what you want, but you would likely want to also use tags as well to let you skip lots of steps which are irrelevant when you only wish to calculate and display the versions.

Not sure if this is exactly what you want to do, but have you tried using variables in your play using register? I.e.

  • name: Get python version
    shell: python -V
    register: python_version

  • name: Show python version
    debug:
    msg: “{{ python_version }}”

This will return the version on each host modified. Sorry if this isn’t related to your question, but seems like that was what you’re looking for.