Combine with_items loop with vars

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

you need to use with_dict instead of with_items

so you vars declaration would look like :

`

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

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…

`

actually you might also want to do it this way

`

Awesome! This works like a charme.

Thanks a lot!
Stephan