Jinja2 - Indentation and recursive loop from a list

Viewed 194

The query is mostly related to Jinja2 to recurse through a loop and add the elements into a recursive JSON type format.

For example, got a pipe separated list/data which is a "pipe separated hierarchy" as below

    World|North_America|USA|Texas
    World|Europe|UK|England|London
    World|Australia

The hierarchy can be of any level and I wanted to have a JSON format based on the hierarchy by using Jinja2 template

What I've tried is

    {% for item in  pipe_separated_hierarchy.split('|') %}
    "obj_{{loop.index}}": {
        "{{item}}": {
            "my_description_obj{{loop.index}}",
            "obj_{{loop.index}}": {
            <<<loop from 2nd iteration again >>> # not sure how to loop.index & item ?
        }
    {% endfor %}

I'm looking for an output

    "obj_1": {
      "World": {
        "obj_2": {
          "North_America": {
            "obj_3": {
              "USA": {
                "obj_4": "Texas"
                }
              }
            }
          }
        }
      }

Any idea how to loop through the list while maintaining the count alongside and indent?

1 Answers

For example

- hosts: localhost
  vars:
    l1:
      - World|North_America|USA|Texas
      - World|Europe|UK|England|London
      - World|Australia
  tasks:
    - debug:
        var: _out|from_yaml
      loop: "{{ l1 }}"
      vars:
        _list: "{{ item.split('|') }}"
        _len: "{{ _list|length }}"
        _out: |
          {% for i in range(_len|int) %}
          {{ 'obj_'|indent(width=(i*4),first=true) }}{{ i+1 }}:
            {{ _list[i]|indent(width=(i*4),first=true) }}{{ ":" if not loop.last else "" }}
          {% endfor %}

gives

ok: [localhost] => (item=World|North_America|USA|Texas) => {
    "_out|from_yaml": {
        "obj_1": {
            "World": {
                "obj_2": {
                    "North_America": {
                        "obj_3": {
                            "USA": {
                                "obj_4": "Texas"
                            }
                        }
                    }
                }
            }
        }
    },
    "ansible_loop_var": "item",
    "item": "World|North_America|USA|Texas"
}
ok: [localhost] => (item=World|Europe|UK|England|London) => {
    "_out|from_yaml": {
        "obj_1": {
            "World": {
                "obj_2": {
                    "Europe": {
                        "obj_3": {
                            "UK": {
                                "obj_4": {
                                    "England": {
                                        "obj_5": "London"
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "ansible_loop_var": "item",
    "item": "World|Europe|UK|England|London"
}
ok: [localhost] => (item=World|Australia) => {
    "_out|from_yaml": {
        "obj_1": {
            "World": {
                "obj_2": "Australia"
            }
        }
    },
    "ansible_loop_var": "item",
    "item": "World|Australia"
}
Related