1

My ansible script is doing a great job at putting all the mounts on any system I specify. But my intention was to install mounts dependent on subnet.

People on .35 are privileged and need acccess to special documents whereas everyone can access our public docs and apps drive.'

Does anyone know what I'm doing wrong here?

[UPDATE] After Vladimir Botka's great reply I'm nearly there. The only problem is my section here (as I've abandoned roles now):

tasks:
    - name: Mount the entries in list
      mount:
        name: '{{ item }}'
        path: '{{ item.mount_point }}'
        src: '{{ item.shared_storage }}'
        fstype: nfs
        opts: rw
        state: mounted
      with_items: '{{ my_mounts }}'

Do you know why its outputting lines into fstab like this:

srvr:/docs {'mount_point':\040'/mnt/doc',\040'shared_storage':\040'srvr:/docs'} nfs rw 0 0 

--- Instead Of ----

srvr:/docs /mnt/doc nfs rw 0 0

?

I tried setting path: '{{ item.mount_point.1 }}' and a few other total guesses but to no avail.

ORIGINALPLAYBOOK WAS:

---

- name: My mounts
  hosts: accounting
  become: yes

  vars:
    subnet: "{{ ansible_default_ipv4.address.split('.')[2:3] | join('.') }}"

  tasks:
    - debug:
        msg: subnet is {{ subnet }}

  roles:
    - role: nfsmount
      vars:
        mount_point: /mnt/docs
        shared_storage: bay05:/docs/spreadsheets
      when: ( "subnet == '20'" ) or ( "subnet == '35'" )

    - role: nfsmount
      vars:
        mount_point: /mnt/pub
        shared_storage: cheetah:/pub
      when: ( "subnet == '20'" )
1
  • 1
    Remove name: '{{ item }}'. See parameter path. name is alias of path. What do you want to achieve by using them both in a single task? May 30, 2022 at 18:38

1 Answer 1

2

For example, put the declarations below as appropriate

    subnet: "{{ ansible_default_ipv4.address.split('.')|last }}"
    nfsmounts:
      20:
        - mount_point: /mnt/pub
          shared_storage: cheetah:/pub
      35:
        - mount_point: /mnt/docs
          shared_storage: bay05:/docs/spreadsheets
        - mount_point: /mnt/pub
          shared_storage: cheetah:/pub
      default:
        - mount_point: /mnt/scratch
          shared_storage: bay09:/scratch
    my_mounts: "{{ nfsmounts[subnet|int]|d(nfsmounts.default) }}"

Then, the tasks

    - debug:
        var: ansible_default_ipv4
    - debug:
        var: subnet
    - debug:
        msg: |-
          mount_points: {{ my_mounts|map(attribute='mount_point')|list }}
          shared_store: {{ my_mounts|map(attribute='shared_storage')|list }}

give

TASK [debug] *****************************************************
ok: [test_11] => 
  ansible_default_ipv4:
    address: 10.3.0.35
ok: [test_12] => 
  ansible_default_ipv4:
    address: 10.3.0.20
ok: [test_13] => 
  ansible_default_ipv4:
    address: 10.3.0.19

TASK [debug] *****************************************************
ok: [test_11] => 
  subnet: '35'
ok: [test_12] => 
  subnet: '20'
ok: [test_13] => 
  subnet: '19'

TASK [debug] *****************************************************
ok: [test_11] => 
  msg: |-
    mount_points: ['/mnt/docs', '/mnt/pub']
    shared_store: ['bay05:/docs/spreadsheets', 'cheetah:/pub']
ok: [test_12] => 
  msg: |-
    mount_points: ['/mnt/pub']
    shared_store: ['cheetah:/pub']
ok: [test_13] => 
  msg: |-
    mount_points: ['/mnt/scratch']
    shared_store: ['bay09:/scratch']

Fit the structures and the code to your needs.

1
  • Thanks sir, I updated my original question
    – bluesquare
    May 30, 2022 at 18:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .