# Expanding var with more than one hostvars reference

**URL:** <https://forum.ansible.com/t/expanding-var-with-more-than-one-hostvars-reference/2423>\
**Category:** Get Help\
**Tags:** playbook, ansible-core\
**Created:** [November 16, 2023, 4:54pm UTC](https://forum.ansible.com/t/expanding-var-with-more-than-one-hostvars-reference/2423 "2023-11-16T16:54:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmarcy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/gmarcy/32/1574_2.png) [@gmarcy](https://forum.ansible.com/u/gmarcy)\
**Post date:** [November 16, 2023, 4:54pm UTC](https://forum.ansible.com/t/expanding-var-with-more-than-one-hostvars-reference/2423/1 "2023-11-16T16:54:17Z")

</div>

**Unexpected behavior in variable expansion**.

I have a small example of the behavior [here](https://github.com/gmarcy/ansible-automation/tree/merge_branch/bug-report). Not sure yet if this is a bug.

Here is a simplified version.

inventory.yml:

```auto
all:
  hosts:
    localhost:
      prefix_url: "prefix"
      inv_value_fact: "{{ hostvars['localhost'].prefix_url + '/special-value' }}"
      inv_value: "{{ inv_value_fact }}"
  vars:
    actual_value: "{{ inv_value }}"

```

I was expecting these two command lines to produce the same result:

`ansible -i inventory.yml -m debug -a var=actual_value all`

```auto
localhost | SUCCESS => {
    "actual_value": "prefix/special-value"
}

```

`ansible -i inventory.yml -m debug -a "var=hostvars['localhost'].actual_value" all`

```auto
localhost | SUCCESS => {
    "hostvars['localhost'].actual_value": "{{ inv_value }}"
}

```

there is also a `play.yml` in the github repo producing the same results.

I am trying to understand how the play was able to load the correct value from the hosts group of the play, but accessing the hostvars value directly will not complete the evaluation. I’ve also determined that if I change the definition of `actual_value` to

```auto
actual_value: "{{ inv_value | default('some-default') }}"

```

the output of  
`ansible -i inventory.yml -m debug -a “var=hostvars[‘localhost’].actual_value” all``  
will change to

```auto
localhost | SUCCESS => {
    "hostvars['localhost'].actual_value": "some-default"
}

```

This was originally seen in a much more complex environment and reported because the default value was being used when the inventory provided a definition that should have overridden that default.

Any insights into why this is occurring would be appreciated.

Thanks,  
Glenn

---

<div class="post-metadata">

**Author:** ![angelmi](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@angelmi](https://forum.ansible.com/u/angelmi)\
**Post date:** [June 6, 2024, 9:52pm UTC](https://forum.ansible.com/t/expanding-var-with-more-than-one-hostvars-reference/2423/2 "2024-06-06T21:52:12Z")

</div>

Did you ever figure this out? I am seeing very similar behavior

---

<div class="post-metadata">

**Author:** ![utoddl](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/utoddl/32/249_2.png) [@utoddl](https://forum.ansible.com/u/utoddl)\
**Post date:** [June 7, 2024, 12:37am UTC](https://forum.ansible.com/t/expanding-var-with-more-than-one-hostvars-reference/2423/3 "2024-06-07T00:37:18Z")

</div>

Caveat: I don’t actually know the answer. However, I’m fairly sure it sits at the intersection of lazy variable evaluation and the contrast between host facts and variables, and I think it’s relevant that the values of ~~facts~~  ~~variables~~ “named things” set through `ansible.builtin.set_fact` are fully evaluated at `set_fact`-time.

I could make further guesses, but the more I did the wronger I’d get. I hope somebody who _actually_ knows will give us a concise answer, because it’s a fascinating question. The number of people who could do that, though, is quite small.

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [June 7, 2024, 1:48pm UTC](https://forum.ansible.com/t/expanding-var-with-more-than-one-hostvars-reference/2423/4 "2024-06-07T13:48:09Z")

</div>

I think this is the same issue described in [hostvars[host][variable] are not recursively templated · Issue #17806 · ansible/ansible · GitHub](https://github.com/ansible/ansible/issues/17806#issuecomment-853930524). There’s a draft fix, but it doesn’t look like it handles this yet (it causes a recursion error). ~~A workaround could be setting `inv_value_fact` anywhere other than the inventory. I tested with `group_vars` and extra vars, both of these seem to work.~~ That’s not right - I inadvertently replaced `inv_value_fact: "{{ hostvars['localhost'].prefix_url + '/special-value' }}"` with `inv_value: "{{ prefix_url + '/special-value' }}"`, which fixes it in inventory too.
