0

In Ansible, if I have a list variable with the following mixed structure:

input_items:
    - mainkey: 'main.value.1'
      subkey: 'subvalue_1.1'

    - mainkey: 'main.value.2'
        group:
            - subkey: 'subvalue_2.1'
            - subkey: 'subvalue_2.2'

Is there a way to transform it to a flat structure like this:

transformed:
    - mainkey: 'main.value.1'
      subkey: 'subvalue_1.1'

    - mainkey: 'main.value.2'
      subkey: 'subvalue_2.1'

    - mainkey: 'main.value.2'
      subkey: 'subvalue_2.2'

The idea being to allow a more compact structure to define the list, then transform it to a flat structure that is simple to feed in to a module using with_items.

I've exhausted every shape of loop, include, set_fact and jinja filter I can muster but I'm sure there must be a way.

1

1 Answer 1

1

As per udondan's suggestion I've created a filter plugin to do just this. Here is a gist with the full python code and example data. I've also suggested it to the ansible google group for inclusion into core. You can comment there if you'd like to see it in core.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.