Enhanced behaviour for mount

Hi all!

I just spent a few hours trying to get mount to work like I wanted it to. I am not mounting anything special, I just want to add the “acl” option to whatever disk is mounted at “/”. This means that I do not know what the source is.

I got some help in ansible on how to get the source. With a little help, I expanded that to being able to extract pass, dump, and options as well. It turns out I need all of these otherwise the mount module will use the defaults.

For example, if I want to set passno to 1 and dump was previously set to 2 it will be reset dump to the default value of 0. This is not very ideal.

My full final solution was:

  • shell: “grep ’ / ’ /etc/mtab | awk ‘{print $1}’”
    register: root_device

  • shell: “grep ’ / ’ /etc/mtab | awk ‘{print $3}’”
    register: root_fstype

  • shell: “OPTS=$(grep ’ / ’ /etc/mtab | awk ‘{print $4}’); if ! $( echo "$OPTS" | tr ‘,’ "\n" | grep -qx ‘acl’ ); then OPTS=$OPTS,acl; fi; echo $OPTS”
    register: root_options

  • shell: “grep ’ / ’ /etc/mtab | awk ‘{print $5}’”
    register: root_dump

  • shell: “grep ’ / ’ /etc/mtab | awk ‘{print $6}’”
    register: root_pass

  • mount: name=/ src={{ root_device.stdout }} fstype={{ root_fstype.stdout }} state=mounted opts={{ root_options.stdout }} dump={{ root_dump.stdout }} passno={{ root_pass.stdout }}

This works well but I end up getting a bunch of change/update requests every time since the shell tasks always have to run even if nothing actually changes.

Here are my questions:

  1. Could mount be updated to read in existing values and populate defaults based on those rather than the current hardcoded defaults?

  2. Could mount be smart enough to accept either a name or a source and guess the rest based on that? Ideally I want to just update the information for whatever is mounted at “/” since I might not know what the source was if I have several machines.

  3. Is there an easier way to do what I’m doing without making any other changes? Seems like this is something that should be doable already so maybe I’m just missing something.

  4. If I wanted to try and implement this, would it be better to try and implement it as another module? I’m thinking maybe “mount_option: name=/ state=present value=acl” to add an option or “mount_option: source=/dev/sdb2 state=missing value=rw” to remove an option.

Thanks!

fyi, ansible facts should have a dict with all mountpoints, options and sources​, you should not need to look at mtab yourself

Modules should take a state=absent to remove the entity, but for modification, should just take a “state=present” (default) with new options.

I’m definitely open to additions (other than the fact things, because they are there).

I’m new to this, so I’ll look into facts for this type of thing in the future. Thanks for the tip! However, ‘ansible_mounts’ doesn’t look to be a dictionary. How would I work with this? Is this appropriate? This works and I’m mostly happy with it (see below) but I’m not sure if I’m still making it more complicated than it needs to be.

  • mount: name={{ item.mount }} src={{ item.device }} fstype={{ item.fstype }} state=mounted opts=“{{ item.options }},acl”
    with_items: ansible_mounts
    when: item.mount == ‘/’
    when: item.options | search(‘^((?!(^acl,|,acl,|,acl$)).)*$’)

Also, it looks like dump and passno are not in the ansible_mounts object. Maybe I can see about adding those?

Thanks again for all of your help!