# Combine with\_items loop with vars

**URL:** <https://forum.ansible.com/t/combine-with-items-loop-with-vars/15842>\
**Category:** Ansible Project\
**Created:** [August 2, 2014, 7:05am UTC](https://forum.ansible.com/t/combine-with-items-loop-with-vars/15842 "2014-08-02T07:05:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephan.rickauer](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@stephan.rickauer](https://forum.ansible.com/u/stephan.rickauer)\
**Post date:** [August 2, 2014, 7:05am UTC](https://forum.ansible.com/t/combine-with-items-loop-with-vars/15842/1 "2014-08-02T07:05:07Z")

</div>

Friends,

I’d like to iterate over ansible\_mounts and set custom options for /some/ mount points, like so

/var: defaults,nosuid  
/home: defaults,nosuid,nodev  
/: defaults

- name: Secure Mount options

mount: name={{ item.mount }} src=‘{{ item.device }}’ state=present fstype={{ item.fstype }} opts={{ ??? }}  
with\_items: ansible\_mounts

How can I combine the list of vars within my loop. So that opts= is filled with the value looked up depending on the mount point?

Thanks for any pointers!  
Stephan

---

<div class="post-metadata">

**Author:** ![Amr\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Amr\_Ali](https://forum.ansible.com/u/Amr_Ali)\
**Post date:** [August 2, 2014, 5:09pm UTC](https://forum.ansible.com/t/combine-with-items-loop-with-vars/15842/2 "2014-08-02T17:09:31Z")

</div>

you need to use with\_dict instead of with\_items

so you vars declaration would look like :

`

---

<div class="post-metadata">

**Author:** ![stephan.rickauer](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@stephan.rickauer](https://forum.ansible.com/u/stephan.rickauer)\
**Post date:** [August 4, 2014, 4:19pm UTC](https://forum.ansible.com/t/combine-with-items-loop-with-vars/15842/3 "2014-08-04T16:19:54Z")

</div>

Hi!

Thanks for your help. I’m still not getting it kinda, but my initial post wasn’t really clearly formulated actually.

I actually like to iterate over the ansible\_mounts object fact that has three mount points. And then use those facts to build my mount tasks, except for the mount _options_, which I’d like to specify myself. So in (broken) shell like pseudo code that would be something like:

for i in ansible\_mounts; do  
task = “mount: name={{ ansible\_mount.mount }} src={{ ansible\_mount.device }} state=present fstype={{ ansible:mount.fstype }}”  
if ansible\_mount.mount -eq ‘/var’  
opts={{ defaults,nosuid }}  
elseif ansible\_mount.mount -eq ‘/home’  
opts={{ defaults,nosuid,nodev }}  
fi  
done

That way, I could specify the mount options dynamically, no matter how many devices are actually present in ansible.mounts. My current solution is rather static, using with\_items like so - and requires labels to be set:

- name: Secure Mount options  
mount: name={{ item.mount }} src=‘LABEL={{ item.label }}’  
state=present fstype={{ item.fs }}  
opts={{ item.options }} passno={{ item.fsck }}  
with\_items:
- { mount: ‘/home’, label: ‘home’, fs: ‘ext4’, fsck: ‘2’, options: ‘defaults,nosuid,nodev’ }
- { mount: ‘/var’, label: ‘var’, fs: ‘ext4’, fsck: ‘2’, options: ‘defaults,nosuid’ }
- { mount: ‘/’, label: ‘root’, fs: ‘ext4’, fsck: ‘1’, options: ‘defaults’ }
- { mount: ‘swap’, label: ‘swap’, fs: ‘swap’, fsck: ‘0’, options: ‘defaults’ }

Is that possible?  
Cheers,  
Stephan

---

<div class="post-metadata">

**Author:** ![Amr\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Amr\_Ali](https://forum.ansible.com/u/Amr_Ali)\
**Post date:** [August 5, 2014, 1:48am UTC](https://forum.ansible.com/t/combine-with-items-loop-with-vars/15842/4 "2014-08-05T01:48:08Z")

</div>

Hi,

didn’t realize you were using the ansible\_mounts fact, i understand what you are trying to do here and i think there might be a way…

how about you try doing this :

Define a new var “mount\_options” which links mount names to options predefined by you, then use it in your mount command to dynamically fill the options argument.

what i mean is this…

`

---

<div class="post-metadata">

**Author:** ![Amr\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/838e76/32.png) [@Amr\_Ali](https://forum.ansible.com/u/Amr_Ali)\
**Post date:** [August 5, 2014, 1:55am UTC](https://forum.ansible.com/t/combine-with-items-loop-with-vars/15842/5 "2014-08-05T01:55:27Z")

</div>

actually you might also want to do it this way

`

---

<div class="post-metadata">

**Author:** ![stephan.rickauer](https://avatars.discourse-cdn.com/v4/letter/s/7feea3/32.png) [@stephan.rickauer](https://forum.ansible.com/u/stephan.rickauer)\
**Post date:** [August 5, 2014, 3:42pm UTC](https://forum.ansible.com/t/combine-with-items-loop-with-vars/15842/6 "2014-08-05T15:42:18Z")

</div>

Awesome! This works like a charme.

Thanks a lot!  
Stephan
