Mount file share based on variable in loop

36 views
Skip to first unread message

Tim Shubitz

unread,
Feb 18, 2021, 5:36:33 PM2/18/21
to Ansible Project
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
      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
      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
      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 }}"

Wei-Yen Tan

unread,
Feb 18, 2021, 5:40:22 PM2/18/21
to ansible...@googlegroups.com
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. 


From: ansible...@googlegroups.com <ansible...@googlegroups.com> on behalf of Tim Shubitz <tshu...@gmail.com>
Sent: Friday, February 19, 2021 11:36:33 AM
To: Ansible Project <ansible...@googlegroups.com>
Subject: [ansible-project] Mount file share based on variable in loop
 
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/0bd14d3d-7c6a-49c2-96d5-6b5b57fdbd8bn%40googlegroups.com.

Wei-Yen Tan

unread,
Feb 18, 2021, 5:54:22 PM2/18/21
to ansible...@googlegroups.com
With a loop var*


From: ansible...@googlegroups.com <ansible...@googlegroups.com> on behalf of Tim Shubitz <tshu...@gmail.com>
Sent: Friday, February 19, 2021 11:36:33 AM
To: Ansible Project <ansible...@googlegroups.com>
Subject: [ansible-project] Mount file share based on variable in loop
 

Tim Shubitz

unread,
Feb 18, 2021, 7:24:22 PM2/18/21
to ansible...@googlegroups.com

> On Feb 18, 2021, at 4:40 PM, Wei-Yen Tan <weiye...@gmail.com> wrote:
>
> 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.

Wei-Yen,
With the quickness!

Looking forward to your code snippets.

Thanks!


- mr. tim

Wei-Yen Tan

unread,
Feb 18, 2021, 8:17:12 PM2/18/21
to ansible...@googlegroups.com
Hi Tim,

data structure in inventory (i use AWX) so just look at os_directory_file_items and mount_list
___

- name: kubernetes workers
    inventory: inventory1
    description: 
    state: present
    children:
      - childnodes
    variables:
      os_directory_file_items:
         - name: mnt
           path: /mnt
           state: directory 

         - name: plceholder1
           path: /mnt/placeholder
           state: directory
        
         - name: Placeholder2
           path: /mnt/Placeholder2
           state: directory

      mount_list:
        - path: '/mnt/placeholder'
          mnt_name: Placeholder1
          src: 'Server1:/volume1//Placeholder1' 
          fstype: nfs
          state: mounted
        
        - path: /mnt/Placeholder2
          mnt_name: Placeholder2
          src: 'Server1:/volume1//Placeholder2' 
          fstype: nfs
          state: mounted


___
main.yml in role
---
  - name: Create Mountpoint on OS
    mount:
       path: "{{ mount_item.path }}"
       fstab: "{{ mount_item.fstab | default(omit)}}"
       boot: "{{ mount_item.boot | default(omit)}}"
       dump: "{{ mount_item.dump | default(0)}}"
       fstype:  "{{ mount_item.fstype | default(0)}}"
       opts: "{{ mount_item.opts | default(omit)}}"
       passno: "{{ mount_item.passno | default(0)}}"
       src: "{{ mount_item.src | default('present')}}"
       state: "{{ mount_item.state | default('mounted')}}"
    loop: "{{ mount_list }}"
    loop_control:
      loop_var: mount_item 
    when: mount_list is defined  
___
playbook
---
- name: Configure common linux settings
  hosts: all
  roles:
    - { role: configure_common_os_directory , when: os_directory_file_items is defined , tags: os_directory_file_settings  } 
    - { role: configure_mnt , when: mount_list is defined , tags: linux_mnt_settings  }

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages