# How to loop over ansible\_mounts\[elements\].mount

**URL:** <https://forum.ansible.com/t/how-to-loop-over-ansible-mounts-elements-mount/15515>\
**Category:** Ansible Project\
**Created:** [June 20, 2014, 12:26pm UTC](https://forum.ansible.com/t/how-to-loop-over-ansible-mounts-elements-mount/15515 "2014-06-20T12:26:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Servermanaged](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Servermanaged](https://forum.ansible.com/u/Servermanaged)\
**Post date:** [June 20, 2014, 12:26pm UTC](https://forum.ansible.com/t/how-to-loop-over-ansible-mounts-elements-mount/15515/1 "2014-06-20T12:26:07Z")

</div>

Dear friends

I need to generate a line in a configuration file based on ansible\_mounts but only when /home mount is present.

I know for instance that I can access ansible\_mounts[1].mount or ansible\_mounts[2].mount but not even ansible\_mounts[1].mount nor ansible\_mounts[2].mount are /home.

My idea is to loop over ansible\_mounts[elements].mount and check whether /home is present but what is not clear to me is how to loop when the numer of elements to loop over is not known in advance.

Kind regards.

---

<div class="post-metadata">

**Author:** ![Ernest0x](https://avatars.discourse-cdn.com/v4/letter/e/ed655f/32.png) [@Ernest0x](https://forum.ansible.com/u/Ernest0x)\
**Post date:** [June 20, 2014, 12:42pm UTC](https://forum.ansible.com/t/how-to-loop-over-ansible-mounts-elements-mount/15515/2 "2014-06-20T12:42:41Z")

</div>

Try something like this: - modulename: … when: item.mount == ‘/home’ with\_items: ansible\_mounts

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [June 20, 2014, 12:45pm UTC](https://forum.ansible.com/t/how-to-loop-over-ansible-mounts-elements-mount/15515/3 "2014-06-20T12:45:35Z")

</div>

quick example:

- hosts: localhost  
&nbsp;&nbsp;connection: local  
&nbsp;&nbsp;tasks:  
&nbsp;&nbsp;&nbsp;&nbsp;- debug: msg={{item.mount}}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;with\_items: ansible\_mounts  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when: item.mount == '/home'

---

<div class="post-metadata">

**Author:** ![Servermanaged](https://avatars.discourse-cdn.com/v4/letter/s/d6d6ee/32.png) [@Servermanaged](https://forum.ansible.com/u/Servermanaged)\
**Post date:** [June 20, 2014, 2:38pm UTC](https://forum.ansible.com/t/how-to-loop-over-ansible-mounts-elements-mount/15515/4 "2014-06-20T14:38:31Z")

</div>

Exactly what I want! Thanks 🙂
