1
votes

So I'm using a playbook based on group_vars, which are used for giving IIS settings for a wide variety of websites.

The group_vars consist of 2 main dicts (one of them containing complex lists of dicts of lists of dicts.)

The complex one has references to vars from firstdict in Jinja2 (e.g. {{ firstdic.sitename }})

This works perfectly with group_vars, references are resolved by the jinja2 engine in Ansible. But when it comes to a dynamic inventory, the whole dict is unset when referencing vars from the first dict.

Here comes some JSON (as from my dynamic inventory) :

"firstdict": { "sitename": "mysitename" }

"complexdict": {"someotherdict": {"sitepath": "{{ firstdict.sitename }}"}}

When using this JSON, the debug module would find that complexdict is not defined.

However, the jinja2 part is correctly parsed and executed as this would work.

Dynamic inventory input :

"complexdict": {"someotherdict": {"sitepath": "{{ \"foo\" }}"}}

Debug module returning :

  "complexdict": {
        "someotherdict": {
            "sitepath": "foo"
        }
    }

Is there a variable precedence problem ? Anything else I should know why it could not work like this ?

Thanks for your help, Ansible masters :)

PS : I've already tried empiric escaping a lot, escaping curly braces, double-quotes, and so on.

1

1 Answers

1
votes

Works for me:

./inventory/test.sh:

#!/bin/bash

cat << EndOfJSON
{
    "test"   : {
    "hosts"  : [ "localhost" ],
    "vars"   : {
      "firstdict": { "sitename": "mysitename" },
      "complexdict": {"someotherdict": {"sitepath": "{{ firstdict.sitename }}"}}
        }
    }
}
EndOfJSON

./playbook.yml

---
- hosts: test
  connection: local
  gather_facts: no
  tasks:
    - debug:
        var: complexdict

output:

TASK [debug] *******************************************************************
ok: [localhost] => {
    "complexdict": {
        "someotherdict": {
            "sitepath": "mysitename"
        }
    }
}

If this is not the case, please update your question with complete MCVE.