0

I define a yml variable files for ansible with the following structure:

appserver:
   root_directory: C:\app
   config_directory: '{{ root_directory }}\config'

it seems the second variable config_directory cannot be interpreted correctly, I get a VARIABLE NOT FOUND ERROR.

I tried with:

appserver:
   root_directory: C:\app
   config_directory: '{{ appserver.root_directory }}\config'

It does not work either, I have a very long trace of error, the most interesting part is :

recursive loop detected in template string:{{ appserver.root_directory }}\config

When I use double quotes instead of simple quotes,

appserver: root_directory: C:\app config_directory: "{{ appserver.root_directory }}\config"

I get the following error:

 The offending line appears to be:

 app_root: D:\WynsureEnvironments\Application
 wynsure_root: "{{ appserver.root_directory }}\config"
                                              ^ here
 We could be wrong, but this one looks like it might be an issue with
 missing quotes.  Always quote template expression brackets when they
 start a value. For instance:

with_items:
  - {{ foo }}

Should be written as:

with_items:
  - "{{ foo }}"

When using variable blocks, how can I reuse variables to assign new variables?

Thanks!

Joel
  • 669
  • 7
  • 25

1 Answers1

3

You cannot use such a recursive jinja2 variable declaration in ansible.

Here are 2 (non exhaustive list) alternative solutions:

  1. Don't use a hash. Prepend your vars names. You will typically find this type of naming conventions in e.g. reusable roles on ansible galaxy
appserver_root_directory: C:\app
appserver_config_directory: '{{ appserver_root_directory }}\config'
  1. If you really need a hash of this kind, declare a "private" variable outside of your hash and reuse it inside.
_appserver_root: C:\app
appserver:
  root_directory: "{{ _appserver_root }}"
  config_directory: "{{ _appserver_root }}\config"
Zeitounator
  • 38,476
  • 7
  • 53
  • 66
  • The mistake is in my post on stackoverflow, I do put the quotes (simple quotes though), and it does not work. I will try with double quotes. – Joel Jun 13 '19 at 13:02
  • With the simple quotes, I get an error, like "recursive loop detected." – Joel Jun 13 '19 at 13:05
  • `it does not work`, `It does not even parse with double quotes` => please edit your post and show us the errors. Also please take the time to read ["how do I create a minimal reproducible example"](https://stackoverflow.com/help/minimal-reproducible-example) – Zeitounator Jun 13 '19 at 13:16
  • Sorry, I overlooked your contents on my first read. You cannot use this type of recursive declaration in jinja2. Updating my answer with alternatives. – Zeitounator Jun 13 '19 at 14:26
  • it seems I cannot use a variable of a block in the same block, right? – Joel Jun 13 '19 at 15:03
  • No, because it is a recursive call (e.g. you are trying to calculate the content of your `appserver` hash while.... already calculating its content....). – Zeitounator Jun 13 '19 at 15:05
  • Ok understood, but it's a setback to describe paths like that. Can I use ansible variables in json files? – Joel Jun 13 '19 at 15:09