# Mount file share based on variable in loop

**URL:** <https://forum.ansible.com/t/mount-file-share-based-on-variable-in-loop/34647>\
**Category:** Ansible Project\
**Created:** [February 18, 2021, 10:36pm UTC](https://forum.ansible.com/t/mount-file-share-based-on-variable-in-loop/34647 "2021-02-18T22:36:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tim\_Shubitz](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@Tim\_Shubitz](https://forum.ansible.com/u/Tim_Shubitz)\
**Post date:** [February 18, 2021, 10:36pm UTC](https://forum.ansible.com/t/mount-file-share-based-on-variable-in-loop/34647/1 "2021-02-18T22:36:33Z")

</div>

Greetings, folks!

I’m trying to have one single source-of-truth for file share info and mount individual shares on different servers depending on need.

Give a list of “these servers get these shares, those get those” and from that list loop over what to configure and mount.

I’ve been trying to use some magic via a ‘when’ conditional to configure and mount but nothing has gelled.

Suggestions would be greatly appreciated.

Thank you for your time and have a wonderful rest of your day!

- mr. tim

Example setup…

vars:  
app\_server\_mounts:

- share0
- share2

utility\_server\_mounts:

- share2

cifs\_file\_share\_mounts:

- path: /mnt/share0  
src\_host: [nas.example.com](http://nas.example.com)  
src\_path: share0  
fstype: cifs  
opts: sec=ntlmsspi,vers=3.0,rsize=61440,wsize=65536  
state: mounted

- path: /mnt/share1  
src\_host: [nas.example.com](http://nas.example.com)  
src\_path: share1  
fstype: cifs  
opts: sec=ntlmsspi,vers=3.0,rsize=61440,wsize=65536  
state: mounted

- path: /mnt/share2  
src\_host: [nas.example.com](http://nas.example.com)  
src\_path: share2  
fstype: cifs  
opts: sec=ntlmsspi,vers=3.0,rsize=61440,wsize=65536  
state: mounted

tasks:

- name: Mount CIFS file shares  
mount:  
path: “{{ item.path }}”  
src: “//{{ item.src\_host }}/{{ item.src\_path }}”  
fstype: cifs  
opts: “{{ item.opts }}”  
state: “{{ item.state | default( ‘mounted’ ) }}”  
with\_items: “{{ cifs\_file\_share\_mounts }}”

---

<div class="post-metadata">

**Author:** ![Wei-Yen\_Tan](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wei-yen_tan/32/4111_2.png) [@Wei-Yen\_Tan](https://forum.ansible.com/u/Wei-Yen_Tan)\
**Post date:** [February 18, 2021, 10:40pm UTC](https://forum.ansible.com/t/mount-file-share-based-on-variable-in-loop/34647/2 "2021-02-18T22:40:07Z")

</div>

I actually do this very thing. I have a role that just loops through a var inside the role. That role just uses the Mount task. With a lip. The role runs off the var exists. I can dig up the structure of it may help. On my phone now.

---

<div class="post-metadata">

**Author:** ![Wei-Yen\_Tan](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wei-yen_tan/32/4111_2.png) [@Wei-Yen\_Tan](https://forum.ansible.com/u/Wei-Yen_Tan)\
**Post date:** [February 18, 2021, 10:54pm UTC](https://forum.ansible.com/t/mount-file-share-based-on-variable-in-loop/34647/3 "2021-02-18T22:54:05Z")

</div>

With a loop var\*

---

<div class="post-metadata">

**Author:** ![Tim\_Shubitz](https://avatars.discourse-cdn.com/v4/letter/t/82dd89/32.png) [@Tim\_Shubitz](https://forum.ansible.com/u/Tim_Shubitz)\
**Post date:** [February 19, 2021, 12:24am UTC](https://forum.ansible.com/t/mount-file-share-based-on-variable-in-loop/34647/4 "2021-02-19T00:24:03Z")

</div>

Wei-Yen,  
&nbsp;&nbsp;With the quickness!

Looking forward to your code snippets.

Thanks!

- mr. tim

---

<div class="post-metadata">

**Author:** ![Wei-Yen\_Tan](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/wei-yen_tan/32/4111_2.png) [@Wei-Yen\_Tan](https://forum.ansible.com/u/Wei-Yen_Tan)\
**Post date:** [February 19, 2021, 1:16am UTC](https://forum.ansible.com/t/mount-file-share-based-on-variable-in-loop/34647/5 "2021-02-19T01:16:44Z")

</div>

Hi Tim,

data structure in inventory (i use AWX) so just look at os\_directory\_file\_items and mount\_list
