Expanding var with more than one hostvars reference

Unexpected behavior in variable expansion.

I have a small example of the behavior here. Not sure yet if this is a bug.

Here is a simplified version.


      prefix_url: "prefix"
      inv_value_fact: "{{ hostvars['localhost'].prefix_url + '/special-value' }}"
      inv_value: "{{ inv_value_fact }}"
    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

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

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

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

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

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.