Help looping over local directories

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:

- 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:

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.

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

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 to execute find command

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