0

ansible_mounts is an array of dictionaries, like so -

    "ansible_mounts": [
       {
           "device": "/dev/mapper/vg_centos67template-lv_root",
           "fstype": "ext4",
           "mount": "/",
           "options": "rw",
           "size_available": 8806977536,
           "size_total": 37458894848,
           "uuid": "N/A"
       },
       {
           "device": "/dev/sda1",
           "fstype": "ext4",
           "mount": "/boot",
           "options": "rw",
           "size_available": 369055744,
           "size_total": 499355648,
           "uuid": "N/A"
       },
   ],  

I need to confirm a set of specific mount points exist, with minimum sizes. I have control of the data structure for this, but for now have structured it similarly -

requiredMounts:  
 - { mount: /tmp,     size_min:   2147483648, }  
 - { mount: /dev/shm, size_min: 204010946560, }  

I was hoping to use a selectattr() filter like a grep, but it isn't available.

fatal: [tstServer]: FAILED! => {"failed": true, "msg": "template error while templating string: no filter named 'selectattr'. String: {{ ansible_mounts | selectattr( 'mount', 'equalto', item.mount ) }}"}

Using jinja 2.6. No idea why selectattr() isn't there.
(That's jenkins output, if anyone cares.)

Happy to use when, fail, assert, with_items, with_nested, combine(), and/or anything else; just haven't found quite the right combination to make it neat. I'd rather it not take a dozen steps - one would be ideal.

Suggestions?

Addendum: I can confirm mount points pretty easily in one step with a fail: ... when: not item.mount|is_mount

...which is interesting, because while /dev/shm shows as a mount point this way (and from command like df & mount) it isn't included in ansible_mounts.

That means I can't just use ansible_mounts to check file sizes in a separate with_nested for /dev/shm. This makes me a sad panda.

Further addendum:

Turns out I have to check one mount point (/tmp) that is generally available through the usual filesystem - best option ansible_mounts! - one (/dev/shm) that is tempfs - easiest seems to be df, which can also handle /tmp - and a bunch that oracle manages, so they are only available through fdisk.

In the end it turns out that I have to check well over a dozen drives per system, but that /tmp is the only one available to ansible_mounts. That being said, my question becomes moot the way I asked it...

Thanks for the input.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36

2 Answers2

0

Why not update Jinja2?

From the documentation:

selectattr() New in version 2.7.

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • Tried, among various other efforts. Haven't figured out exactly how in this environment. `Requirement already satisfied: Jinja2 in /usr/lib/python2.6/site-packages/Jinja2-2.6-py2.6.egg` – Paul Hodges Sep 25 '17 at 15:16
  • 1
    `pip install Jinja2==2.8`? – Konstantin Suvorov Sep 25 '17 at 16:05
  • You know, I spent at least an hour trying to find that specific syntax and just kept going in circles. Thanks! :) But `{{ ansible_mounts }}` still not showing my `/dev/shm` mount, so I have to find another way to check the size. :( – Paul Hodges Sep 25 '17 at 17:09
  • You solved the problem I asked, even if I asked the wrong question. ;) – Paul Hodges Sep 28 '17 at 18:05
0

Have an answer to the first part.

- name: testing for required mount points
  fail:
    msg: "{{ item.mount }} must be a mount point"
  when: not item.mount|is_mount
  with_items: "{{ requiredMounts }}"  

Still having a hard time finding a simple, dependable, non-script way to git mount point sizes, though. {{ ansible_mounts }} doesn't show my shared memory /dev/shm mount, and nothing else gives me sizes.

Why would it not include that mount is df shows it?

Is there another way to access mount point allocations without a shell call?

Using a short script, I can do this with -

- name: check mount point space allocation
  shell: df -B 1 {{ reqMount.mount }} | grep {{ reqMount.mount }} | { read x siz x; echo $siz; }
  register: mountStat

- name: Confirm mount point minimum size
  fail:
    msg: "mountpoint '{{ reqMount.mount }}' of {{ mountStat.stdout }} bytes must be at least {{ reqMount.size_min }} bytes."
  when: mountStat.stdout|int < reqMount.size_min|int  

...but I don't like having to use a bit of scripting that I think probably ought to have several lines of comments to explain it. (Should I bother to mention awk makes me feel dirty?)

Also tried the parted module, but my logical mount points are filesystem directories, and I don't see anything but df which easily gives me the partition per dir, so there's little reason to add steps if I'm calling that anyway.

As an addendum, the reason ansible_mounts isn't showing /dev/shm is probably because it's a tmpfs mount. Hope that at least helps someone out.

Paul Hodges
  • 13,382
  • 1
  • 17
  • 36