A question of inventory structure

Hello,

I’ve been writing with ansible for a while now. However, I have an unanswered question.
I’m using a rather complex inventory structure that calls for several roles, some of whose values may be shared.

For example, several roles use
hosts.yml

all:
  children:
    groupe1:
      hosts:
        test[1:2]:
    groupe2:
      children:
        g2_g1:
          hosts:
            test4:
        g2_g2:
          hosts:
            test5:

group_vars/groupe1.yml

variable1_a: "group_1"

test_dict:
  list1:
    - item1:
    - item2:
  var1: ""
  var2: "{{ variable1_a }}"

test1_dict:
  var1: "{{ variable1_a }}" # value group_1
  var2: "{{ variable1_a }}_chaine1" # Value group_1_chaine1

this syntax doesn’t work, but it’s to show you the idea of having dictionaries linked to flat variables

I hope my need is clear.
Thank you very much for your explanations and knowledge

Seems like the syntax does work, and does exactly what you want.
Using a file structure like

hosts.yml
group_vars/
  groupe1.yml

And then running…

~/git/test $ ansible -i hosts.yml -m debug -a 'var=test1_dict' all
test1 | SUCCESS => {
    "test1_dict": {
        "var1": "group_1",
        "var2": "group_1_chaine1"
    }
}
test2 | SUCCESS => {
    "test1_dict": {
        "var1": "group_1",
        "var2": "group_1_chaine1"
    }
}
test5 | SUCCESS => {
    "test1_dict": "VARIABLE IS NOT DEFINED!"
}
test4 | SUCCESS => {
    "test1_dict": "VARIABLE IS NOT DEFINED!"
}

Is that not what you expect?

1 Like

Thank you,
Until now I had not thought of using ansible to test I was using ansible-galaxy which does not seem to substitute the values.
Thanks again for your help I will now look at my inventory structure to make it more readable.
Have a nice day