It would be easier to rewrite your var to something like:

part:
      - name: eyes
        where: faces
        colors:
          - blue
          - brown
          - green

And then use:

    - debug:
        msg: "{{ item.0.name }}_{{ item.1 }}"
      loop: "{{ part|subelements('colors') }}"
      loop_control:
        label: "{{ item.0.name }}_{{ item.1 }}"

Otherwise, using your current data structure:

    - debug:
        msg: "{{ item.0.0 }}_{{ item.1 }}"
      loop: "{{ part|dictsort|subelements([1, 'colors']) }}"
      loop_control:
        label: "{{ item.0.0 }}_{{ item.1 }}"

>From there, you should be able to extrapolate to meet your specific needs,
potentially using the template module, to write out the files.

On Mon, Jun 10, 2019 at 11:24 AM Josh Smift <jsm...@care.com> wrote:

> I have a data structure like this:
>
> part:
>   eyes:
>     where: faces
>     colors:
>       - blue
>       - brown
>       - green
>   hair:
>     where: heads
>     colors:
>       - black
>       - blond
>       - brown
>       - red
>   toenails:
>     where: feet
>     colors:
>       - pink
>       - red
>       - teal
>
> I want to create a file for each of the colors in the nested lists, with a
> name like /etc/parts/eyes_blue, and in that template, have things like
> "This is the file about things with blue eyes on their faces" or "This is
> the file about things with red toenails on their feet" (in
> /etc/parts/toenails_red). So I don't want to loop over the part dict, or
> the various dicts in the part dict, but over the colors lists -- but I need
> to do that while also keeping track of where in the data structure I am, so
> I don't want to just flatten them into one big list and iterate over it. If
> I were writing code for this, I'd do something like
>
> for (key, value) in part.items():
>   for color in value['colors']:
>     with open ("foodir/{}_{}".format(key, color), 'w') as fh:
>       fh.write("This is the file about things with {} {} on their {}".
> format(color, key, value['where']))
>
> Is there a way to do that in an Ansible task somehow?
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ansible-project+unsubscr...@googlegroups.com.
> To post to this group, send email to ansible-project@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ansible-project/f548134f-8981-4f47-8f0c-d76d6b550df0%40googlegroups.com
> <https://groups.google.com/d/msgid/ansible-project/f548134f-8981-4f47-8f0c-d76d6b550df0%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>


-- 
Matt Martz
@sivel
sivel.net

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/CAD8N0v81khQd7djZKmzCdBPq7JmiBrBUCXzZxeOAV1bGmdepuA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to