# Nested directory walking question

**URL:** <https://forum.ansible.com/t/nested-directory-walking-question/15747>\
**Category:** Ansible Project\
**Created:** [July 23, 2014, 5:30pm UTC](https://forum.ansible.com/t/nested-directory-walking-question/15747 "2014-07-23T17:30:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bill\_Lubanovic](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bill\_Lubanovic](https://forum.ansible.com/u/Bill_Lubanovic)\
**Post date:** [July 23, 2014, 5:30pm UTC](https://forum.ansible.com/t/nested-directory-walking-question/15747/1 "2014-07-23T17:30:42Z")

</div>

I’d like to walk a directory hierarchy of the form / **//.**  **is a fixed root directory, and isn’t relevant. I have a number of directories, which represent machine categories. Under each are zero or more directories, which are string values for that category. Finally, is a directory hierarchy to copy to machines that match the parent / combination. For example:**

 

/tmp/test/color/red/etc/passwd  
/tmp/test/color/green/etc/motd  
/tmp/test/flavor/cinnamon/var/something

 

here is /tmp/test. There are two **categories (color and flavor): the first (color) has two subdirectories (red and green) and the second (flavor) has one (cinnamon). Each has a different (file hierarchy) to be copied to machines that match /.**

 

How do I walk each **, passing its value, its subdirectories, and their subdirectories to a task? I need to consider and together with other vars and facts (machine “colors” and “flavors”) to pick target destinations for .**

 

I’ve looked at the various loop constructs, and see how to chain with register, but not how to populate the lists for with\_nested.

 

Thanks!

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 23, 2014, 10:18pm UTC](https://forum.ansible.com/t/nested-directory-walking-question/15747/2 "2014-07-23T22:18:19Z")

</div>

So there’s already “with\_fileglob” in Ansible

- debug: msg={{ path }}  
with\_fileglob: /path/to/files/_/_.tgz

This isn’t recursive however, despite being able to specify multiple directories to read.

It would be possible to make a lookup plugin that was, like a “with\_fileglob\_recursive” that used the results of something like os.path.walk.

So basically yes, you need a new one.

With\_nested is for constructing nested loops and is something a bit different.

---

<div class="post-metadata">

**Author:** ![Bill\_Lubanovic](https://avatars.discourse-cdn.com/v4/letter/b/a9adbd/32.png) [@Bill\_Lubanovic](https://forum.ansible.com/u/Bill_Lubanovic)\
**Post date:** [July 24, 2014, 2:05pm UTC](https://forum.ansible.com/t/nested-directory-walking-question/15747/3 "2014-07-24T14:05:17Z")

</div>

Thanks, Michael. It looks like with\_fileglob() only shows files, not directories, so that wouldn’t work for me.

I’m testing a workaround using “find” or “ls”, then split() to grab **, , and . It would be nice to pass and as arguments to a task using the synchronize module.**

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 24, 2014, 10:45pm UTC](https://forum.ansible.com/t/nested-directory-walking-question/15747/4 "2014-07-24T22:45:45Z")

</div>

I wouldn’t shell out.

I’d call python’s very handy os.path.walk with a custom function. If you’d like to talk about this, ansible-devel is probably a bit more on topic (and I’m trying to spur that list into greater life).
