'Ansible playbook with JSON output

I'm new to Ansible and I'm having a problem passing a variable into json_query to read elements from a JSON array.

working line:

    Interface:{{ result.json | json_query('[2]')}}"

I would like to replace number 2 for a variable that will change.

I tried:

- name: Interfaces
  debug:
    msg:
    - Interface:{{ result.json | json_query('[ item ]')}}"
  with_sequence: start=2 end=5

but it failed.

Thanks in advance!



Solution 1:[1]

I believe you were very close, but what you really want is:

- debug:
    msg:
    - Interface: "{{ result.json | json_query('[' + item + ']') }}"
  with_sequence: count=3

You were correct that inside the jinja2 mustaches item is a variable, but python does not auto-concatenate things, so you must expressly ask for "[" and item to be joined together

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mdaniel