# A question of inventory structure

**URL:** <https://forum.ansible.com/t/a-question-of-inventory-structure/10222>\
**Category:** Get Help\
**Tags:** inventory\
**Created:** [October 12, 2024, 6:25am UTC](https://forum.ansible.com/t/a-question-of-inventory-structure/10222 "2024-10-12T06:25:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexb81](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alexb81/32/5126_2.png) [@alexb81](https://forum.ansible.com/u/alexb81)\
**Post date:** [October 12, 2024, 6:25am UTC](https://forum.ansible.com/t/a-question-of-inventory-structure/10222/1 "2024-10-12T06:25:11Z")

</div>

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

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

```

group\_vars/groupe1.yml

```auto
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

---

<div class="post-metadata">

**Author:** ![mikemorency](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/mikemorency/32/2672_2.png) [@mikemorency](https://forum.ansible.com/u/mikemorency)\
**Post date:** [October 12, 2024, 1:38pm UTC](https://forum.ansible.com/t/a-question-of-inventory-structure/10222/2 "2024-10-12T13:38:34Z")

</div>

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

```auto
hosts.yml
group_vars/
  groupe1.yml

```

And then running…

```auto
~/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?

---

<div class="post-metadata">

**Author:** ![alexb81](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/alexb81/32/5126_2.png) [@alexb81](https://forum.ansible.com/u/alexb81)\
**Post date:** [October 14, 2024, 7:47am UTC](https://forum.ansible.com/t/a-question-of-inventory-structure/10222/3 "2024-10-14T07:47:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [November 13, 2024, 7:47am UTC](https://forum.ansible.com/t/a-question-of-inventory-structure/10222/4 "2024-11-13T07:47:21Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
