The requirement was to get a dynamic set of files from a directory based on a dictionary. I've implemented it with concept mentioned in: Nested loop with a list and a dictionary
- name: color print - hardcoded
debug:
msg:
- "{{ item[0] }} - {{ item[1] }}"
with_nested:
- [ 'red', 'green' ]
- [ 'grapes', 'apple', 'chilli' ]
The above works correctly
But if I change the 2nd list to a dynamically generated list from the 1st list, it is not working. it throws 'item' is undefined" error
Below is a rough implementation I'm trying and getting the error
- name: color print - dynamic
debug:
msg:
- "Copy from directory: {{ item[0] }} to {{ item[1] }}"
with_nested:
- [ 'red', 'green' ]
- "{{colored_fuit| dictsort}}"
vars:
- color: "{{ item[0] }}"
- colored_fuit:
fruit1: "{{color}}_grapes"
fruit2: "{{color}}_apple"
fruit3: "{{color}}_chilli"
Why is it so? It is because the variable is not generated in case of 2nd list?
vars:, where you are trying to definecolored_fruit:but alsowith_nested:over them; it seems you need to move thered, greeninto a separate step and generatecolored_fruit:in that step"{{ item.0 }} to {{ item.0 }}_{{ item.1 }}". I deleted the answer because I didn't know what you want. You commented:filter conditions from 1st list. What is the condition? You commented:nesting needs to ensure the specific set only. What is the expected specific set? I asked you to edit the question and make it minimal reproducible example.