Implicit use of inventory script

Hi,

I have been experiencing a rather peculiar issue when using an in-house dynamic inventory (output here: https://gist.github.com/oboukili/4bfc95d97fc340e770321201f70dce4c ) in conjunction with static group_vars files.
When using this simple playbook:

---
- hosts: infrastructure
  become: true
  pre_tasks:
    - name: debug
      debug:
        msg: "{{ hostvars[item] }}"
      with_items: "{{ groups['infrastructure'] }}"
...

## Invocation:

ansible-playbook -i ~/Workspaces/ansible-inventory/mycloud includes/test.yml

## Output:

PLAY [infrastructure] ***********************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************
ok: [apps-public1]
ok: [bastion]

TASK [debug] ********************************************************************************************************************************************************************************************************
fatal: [apps-public1]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.hostvars.HostVars object' has no attribute u'apps-public1'\n\nThe error appears to have been in '/home/bki/Workspaces/playbooks/test.yml': line 5, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  pre_tasks:\n    - name: debug\n      ^ here\n"}
fatal: [bastion]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'ansible.vars.hostvars.HostVars object' has no attribute u'apps-public1'\n\nThe error appears to have been in '/home/bki/Workspaces/playbooks/test.yml': line 5, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n  pre_tasks:\n    - name: debug\n      ^ here\n"}

When I delete the group_vars folder, everything runs fine (but I lose, of course all the static configuration).
More specifically, I did a lot of debugging with the use of the inventory script and without, and I pinpointed the issue to the ansible.template.AnsibleJ2Vars.get_item() getter (ansible/template.vars.py) that silently fails on this particular step when mixing dynamic inventory script and static group_vars files:

variable = self._templar._available_variables[varname]".

(its value would then not be a hostvars object instance, but a string:
“Unable to get repr for <class ‘ansible.vars.hostvars.HostVars’>”)

Could anybody have pointers?

Thanks a lot,

–Olivier

Replying to myself as I have figured out the issue (but I still do not understand it though).

I had this hash in the group_vars/infrastructure.yml file:

telegraf_agent_output: - type: influxdb config: - urls = [“{{ hostvars[groups[‘influxdb’][0]][‘influxdb_url’] }}”] - database = “telegraf” - precision = “us” - retention_policy = “” - timeout = “10s” - username = “telegraf” - password = “{{ hostvars[groups[‘influxdb’]|random][‘influxdb_secrets’][‘telegraf’] }}” - user_agent = “telegraf_{{ inventory_hostname }}”

Replacing the |random part by a list index fixed it:
- password = “{{ hostvars[groups[‘influxdb’][0][‘influxdb_secrets’][‘telegraf’] }}”

I’ll open an issue with a simple reproductible case later.

Cheers,