# Looping With 'with\_nested' - Wrong Syntax?

**URL:** <https://forum.ansible.com/t/looping-with-with-nested-wrong-syntax/46031>\
**Category:** Get Help\
**Created:** [July 10, 2026, 8:09am UTC](https://forum.ansible.com/t/looping-with-with-nested-wrong-syntax/46031 "2026-07-10T08:09:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dulux-Oz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulux-oz/32/9047_2.png) [@Dulux-Oz](https://forum.ansible.com/u/Dulux-Oz)\
**Post date:** [July 10, 2026, 8:09am UTC](https://forum.ansible.com/t/looping-with-with-nested-wrong-syntax/46031/1 "2026-07-10T08:09:28Z")

</div>

Hi All,

I know there’s a simple fix for this, but I’ve been running 'round the Net for hours now and I can’t crack this issue - could someone please point me in the right direction?

**vars/main.yml File:**

```auto
variable_one:
  - var_1_1
  - var_1_2

variable_two:
  - var_2_1
  - var_2_2

```

**task File:**

```auto
- name: Copy Template Files
    ansible.builtin.template:
      src: "my_source/mytemplate_{{item.0}}.j2"
      dest: "my_destination/{{item.0}}_{{item.1}}.txt"
      owner: me
      group: me
      mode: "0640"
    with_nested:
      - "{{variable_one}}"
      - "{{variable_two}}"

```

**Expected Output (in no particular order):**

```auto
my_destination/var_1_1_var_2_1.txt
my_destination/var_1_1_var_2_2.txt"
my_destination/var_1_2_var_2_1.txt"
my_destination/var_1_2_var_2_2.txt"

```

**Issue:**

The ‘Copy Template Files’ task is trying to find:

```auto
my_source/mytemplate_[.j2
my_source/mytemplate_v.j2
my_source/mytemplate_a.j2
my_source/mytemplate_r.j2
...

```

In other words, its looping over the individual characters of `var_1_1` and `var_1_2` instead of looping of `var_1_1` and `var_1_2`.

So my syntax is wrong (or I’m using the wrong Ansible function).

Any ideas?

Thanks in Advance  
Dulux Oz

---

<div class="post-metadata">

**Author:** ![vbotka](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vbotka/32/359_2.png) [@vbotka](https://forum.ansible.com/u/vbotka)\
**Post date:** [July 10, 2026, 12:40pm UTC](https://forum.ansible.com/t/looping-with-with-nested-wrong-syntax/46031/2 "2026-07-10T12:40:53Z")

</div>

In 2.21, the loop

```yaml
- debug:
    msg: |
      src: "my_source/mytemplate_{{ item.0 }}.j2"
      dest: "my_destination/{{ item.0 }}_{{ item.1 }}.txt"
  with_nested:
    - "{{ variable_one }}"
    - "{{ variable_two }}"

```

Works as expected

```yaml
ok: [localhost] => (item=['var_1_1', 'var_2_1']) => 
    msg: |-
        src: "my_source/mytemplate_var_1_1.j2"
        dest: "my_destination/var_1_1_var_2_1.txt"
ok: [localhost] => (item=['var_1_1', 'var_2_2']) => 
    msg: |-
        src: "my_source/mytemplate_var_1_1.j2"
        dest: "my_destination/var_1_1_var_2_2.txt"
ok: [localhost] => (item=['var_1_2', 'var_2_1']) => 
    msg: |-
        src: "my_source/mytemplate_var_1_2.j2"
        dest: "my_destination/var_1_2_var_2_1.txt"
ok: [localhost] => (item=['var_1_2', 'var_2_2']) => 
    msg: |-
        src: "my_source/mytemplate_var_1_2.j2"
        dest: "my_destination/var_1_2_var_2_2.txt"

```

([source](https://github.com/vbotka/ansible_qa/tree/master/tasks/forum-46031))

---

<div class="post-metadata">

**Author:** ![Dulux-Oz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/dulux-oz/32/9047_2.png) [@Dulux-Oz](https://forum.ansible.com/u/Dulux-Oz)\
**Post date:** [July 14, 2026, 5:52am UTC](https://forum.ansible.com/t/looping-with-with-nested-wrong-syntax/46031/3 "2026-07-14T05:52:06Z")

</div>

Thanks @vbotka
