# Help looping over local directories

**URL:** <https://forum.ansible.com/t/help-looping-over-local-directories/40636>\
**Category:** Get Help\
**Tags:** template, loop, ubuntu\
**Created:** [February 26, 2025, 12:23pm UTC](https://forum.ansible.com/t/help-looping-over-local-directories/40636 "2025-02-26T12:23:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vinylen](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/vinylen/32/7739_2.png) [@vinylen](https://forum.ansible.com/u/vinylen)\
**Post date:** [February 26, 2025, 12:23pm UTC](https://forum.ansible.com/t/help-looping-over-local-directories/40636/1 "2025-02-26T12:23:22Z")

</div>

Hello! Our company is moving to Ansible from SaltStack and i’ve been tearing my hair out over this one thing that i cannot really seem to figure out how to do!

Basically this is the code:

```auto
- name: Template example pipeline input
  ansible.builtin.template:
    src: "{{ item }}"
    dest: "/opt/docker/fluent-bit/{{ logging_fluent_bit_role }}/pipelines/{{ item.path | basename }}"
    owner: ubuntu
    group: ubuntu
    mode: "0655"
  with_fileglob:
    - "templates/pipelines/example/*"
  notify: Restart Fluent Bit container

```

We are using roles so this is the basic filestructure:

```auto
llt -D -L2
.
├── defaults
├── files
│ ├── agent
│ └── aggregator
├── handlers
├── tasks
└── templates
    ├── config
    └── pipelines
        ├── example
        │ └── input
        └── not_this_one
            └── input

```

So what i want to achieve is for ansible.builtin.template to find all directories under “templates/pipelines/example/”, render the template and send it to it’s `dest` on the target.

However the list of directories in “templates/pipelines/example” is always empty. I’ve been googling around on how to loop through directories in ansible but most of the answers are related to finding files.

```auto
TASK [../roles/logging_fluent_bit : Template example pipeline input] *************************************************************************************
task path: /home/vinylen/work/ansible/ansible-environment/roles/logging_fluent_bit/tasks/main.yml:66
looking for "templates/pipelines/example" at "/home/vinylen/work/ansible/ansible-environment/roles/logging_fluent_bit/files/templates/pipelines/example"
looking for "templates/pipelines/example" at "/home/vinylen/work/ansible/ansible-environment/roles/logging_fluent_bit/templates/pipelines/example"
skipping: [example-server.example.com] => {
    "changed": false,
    "skipped_reason": "No items in the list"
}

```

I would greatly appreciate any help.

Thanks

---

<div class="post-metadata">

**Author:** ![kristianheljas](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kristianheljas/32/7702_2.png) [@kristianheljas](https://forum.ansible.com/u/kristianheljas)\
**Post date:** [February 26, 2025, 1:10pm UTC](https://forum.ansible.com/t/help-looping-over-local-directories/40636/2 "2025-02-26T13:10:43Z")

</div>

`fileglob` lookup only matches files, not directories.

Even it this would work, the task would try to use directory as template source which wouldn’t work either.

If you are still looking to list directories, try [pipe lookup](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/pipe_lookup.html) to execute `find` command

```yaml
loop: "{{ lookup('ansible.builtin.pipe', 'find \"' ~ role_path ~ '/templates/pipelines/example\" -type d -mindepth 1 -maxdepth 1') | ansible.builtin.split }}"

```
