ansible – variable within variable


Ansible 1.9.2 version.

Does Ansible supports variable expansion within a variable while evaluating it.

I have a task to download 3 zip files from Artifactory.

Instead of writing 3 separate tasks within the role, I used ansible’s loop in the playbook. In Ansible role’s default/main.yml, I have all the required variables defined/available to the role i.e. jmeterplugins_extras_artifactory_url and other (standard / webdriver) are visible to perf_tests role.

But with the above code, I’m getting an error (as shown below):

Doesn’t ansible supports variable expansion/evaluation if a variable contains another variable (especially when I’m using a loop).

I just dont want to expand my simple loop task into 3 different -name tasks for downloading zip files for jmeterplugins_extras, jmeterplugins_standard and jmeterplugins_webdriver separately. It seems like the error is related due to Jinja.

How can I use var’s value giga in another variable i.e. if var contains giga, then I should get the value of variable “special_giga_variable” ({{special_{{ var }}_variable}})? where var was defined in defaults/main.yml as:

var: giga


No it doesn’t. But it doesn’t mean that you have to expand it into 3 different tasks. What you can do is actually expand you “dictionary” to look similar to this:

Then in your task just call different parameters: {{ item.url }} and {{ item.file }}

Alternative Options:

  1. Write your own filter that will expand your variable according to the value {{ jmeterplugins_url | my_custom_filter(item.plugin) }}
  2. Write a custom module, that will encapsulate all of the functionality of fetching url into the file based on your inputs
  3. Write custom lookup_plugin that will iterate through your list of variables and produce correct result.
  4. Since you are using command module you can leverage bash to concatenate your url, file in the same command ( this would probably be the messiest solution )

Leave a Reply