# report for mount size\_available

**URL:** <https://forum.ansible.com/t/report-for-mount-size-available/34494>\
**Category:** Ansible Project\
**Created:** [January 22, 2021, 4:29pm UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494 "2021-01-22T16:29:41Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [January 22, 2021, 4:29pm UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/1 "2021-01-22T16:29:41Z")

</div>

Hi

I would like to create report for free partition size under “ansible\_mounts”

“ansible\_mounts”: [  
{  
“block\_available”: 3911145,  
“block\_size”: 4096,  
“block\_total”: 4452864,  
“block\_used”: 541719,  
“device”: “/dev/mapper/vg\_root-lv\_root”,  
“fstype”: “xfs”,  
“inode\_available”: 8871855,  
“inode\_total”: 8910848,  
“inode\_used”: 38993,  
**“mount”: “/”,**  
“options”: “rw,seclabel,relatime,attr2,inode64,noquota”,  
**“size\_available”: 16020049920,**  
“size\_total”: 18238930944,  
“uuid”: “e2968252-bcd8-45ac-9604-714aa932deb9”  
},  
{  
“block\_available”: 222270,  
“block\_size”: 4096,  
“block\_total”: 259584,  
“block\_used”: 37314,  
“device”: “/dev/sda1”,  
“fstype”: “xfs”,  
“inode\_available”: 523988,  
“inode\_total”: 524288,  
“inode\_used”: 300,  
**“mount”: “/boot”,**  
“options”: “rw,seclabel,relatime,attr2,inode64,noquota”,  
**“size\_available”: 910417920,**  
“size\_total”: 1063256064,  
“uuid”: “bcce38b2-0c6c-44aa-99be-20e3553d23ee”

how can I get variables for /, /var /boot and other partitions and create report like this in a file:

root: 10GB free  
/boot: 800MB free

/var: 2GB free

Thanks

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 22, 2021, 7:39pm UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/2 "2021-01-22T19:39:07Z")

</div>

- copy:  
&nbsp;&nbsp;&nbsp;&nbsp;content: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for mt in ansible\_mounts %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ mt.mount }}: {{ ( mt.size\_available / ( 1024 | pow(3))) | round(2) }} GB free  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}  
&nbsp;&nbsp;&nbsp;&nbsp;dest:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/tmp/freespace.txt

This creates a report in /tmp/freespace.txt with the GB values with two decimal spaces.  
Distinguishing between GB and MB would be a bit more complicated.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [January 23, 2021, 12:11pm UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/3 "2021-01-23T12:11:07Z")

</div>

Thanks, this works great, can you explain me more what this jinja2 template do especially:

/ ( 1024 | pow(3))) | round(2) }

/ ( 1024 | pow(3)) → is this divide1024 three times? what is pow ?

round(2) → two decimal ?

where can I get more jinja2 examples or tutorial?

Thanks

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 24, 2021, 9:16am UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/4 "2021-01-24T09:16:56Z")

</div>

> Thanks, this works great, can you explain me more what this jinja2 template do especially:
> 
> / ( 1024 | pow(3))) | round(2) }
> 
> / ( 1024 | pow(3)) --\> is this divide1024 three times? what is pow ?  
> round(2) --\> two decimal ?
> 
> where can I get more jinja2 examples or tutorial?

1024 | pow(3) ... that is 1024³ = 1024 \* 1024 \* 1024 = 1 GB

round ... rounds a number with optional decimals, so 10.445 | round(2) =\> 10.45.

So we divide the available size by 1 GB and round it up. The parentheses are important  
to process in the correct orders.

I maintain a couple of filter examples at  
[https://wiki.linuxia.de/library/stefan-hornburg-racke-ansible-provisioning-en#text-amuse-label-filters](https://wiki.linuxia.de/library/stefan-hornburg-racke-ansible-provisioning-en#text-amuse-label-filters)

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [January 24, 2021, 12:24pm UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/5 "2021-01-24T12:24:48Z")

</div>

what will be jinja template if I would like to create report only for / and /var partition? Or haw would I accomplish that?

Thanks

Regards

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 24, 2021, 12:54pm UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/6 "2021-01-24T12:54:51Z")

</div>

> what will be jinja template if I would like to create report only for / and /var partition? Or haw would I accomplish that?
> 
> Thanks
> 
> Regards

You have (at least) two options here.

First with a if condition:

{% for mt in ansible\_mounts %}  
{% if mt.mount in ['/', '/var'] %}  
{{ mt.mount }}: {{ ( mt.size\_available / ( 1024 | pow(3))) | round(2) }} GB free  
{% endif %}  
{% endfor %}

Second with a filter on ansible mounts:

{% for mt in (ansible\_mounts | selectattr('mount', 'search', '^/(var)\*$') | list) %}  
{{ mt.mount }}: {{ ( mt.size\_available / ( 1024 | pow(3))) | round(2) }} GB free  
{% endfor %}

If the partitions to report vary between host / groups use the first solution and replace ['/', '/var']  
with a variable, e.g.

report\_parts:  
&nbsp;&nbsp;- /  
&nbsp;&nbsp;- /var

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [January 26, 2021, 1:35pm UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/7 "2021-01-26T13:35:30Z")

</div>

Hi

I was trying to make report for free space in vg, but I have a problems, it’s look like jinja doesn’t like me::))

ansible\_lvm

},  
“vgs”: {  
“vg\_root”: {  
“free\_g”: “0”,  
“num\_lvs”: “2”,  
“num\_pvs”: “1”,  
“size\_g”: “19.00”  
},  
“vgswap”: {  
“free\_g”: “1.50”,  
“num\_lvs”: “1”,  
“num\_pvs”: “1”,  
“size\_g”: “1.75”

},  
“vgdata”: {  
“free\_g”: “1.80”,  
“num\_lvs”: “1”,  
“num\_pvs”: “1”,  
“size\_g”: “1.75”

- name: vg free  
copy:  
content: |  
{% for vg in ansible\_lvm %}  
{{ vg.vgs }} {{ vg.free\_g }} GB  
{% endfor %}  
dest: /tmp/vg-free-vgs

but it keep telling me that I didn’t defined variable vgs: (The task includes an option with an undefined variable. The error was: ‘str object’ has no attribute ‘vgs’) .

Can you please help me once again with this? Thanks

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 26, 2021, 1:54pm UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/8 "2021-01-26T13:54:41Z")

</div>

> Hi
> 
> I was trying to make report for free space in vg, but I have a problems, it's look like jinja doesn't like me::))  
> ansible\_lvm  
> },  
> "vgs": {  
> "vg\_root": {  
> "free\_g": "0",  
> "num\_lvs": "2",  
> "num\_pvs": "1",  
> "size\_g": "19.00"  
> },  
> "vgswap": {  
> "free\_g": "1.50",  
> "num\_lvs": "1",  
> "num\_pvs": "1",  
> "size\_g": "1.75"  
> },  
> "vgdata": {  
> "free\_g": "1.80",  
> "num\_lvs": "1",  
> "num\_pvs": "1",  
> "size\_g": "1.75"
> 
> -name:vg free  
> copy:  
> content:|  
> {%for vg in ansible\_lvm %}  
> {{vg.vgs }}{{vg.free\_g }}GB  
> {%endfor %}  
> dest:/tmp/vg-free-vgs
> 
> but it keep telling me that I didn't defined variable vgs: (The task includes an option with an undefined variable. The  
> error was: 'str object' has no attribute 'vgs') .

ansible\_lvm is a dictionary and not a list. So looping as above doesn't work.

Please try the following loop:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for vg in ansible\_lvm.vgs | dict2items %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{{ vg.key }} {{ vg.value.free\_g }} GB  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [January 27, 2021, 11:10am UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/9 "2021-01-27T11:10:40Z")

</div>

Thanks it’s working.

The same is this:

- name: vg space  
lineinfile:  
path: /tmp/vg\_free  
line: “VG {{ item.key }} free {{ item.value.free\_g }} GB”  
loop: “{{ ansible\_lvm.vgs| dict2items }}”

Regards

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 27, 2021, 1:08pm UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/10 "2021-01-27T13:08:50Z")

</div>

> Thanks it's working.
> 
> The same is this:
> 
> -name: vg space  
> lineinfile:  
> path:/tmp/vg\_free  
> line:"VG {{ item.key }} free {{ item.value.free\_g }} GB"  
> loop:"{{ ansible\_lvm.vgs| dict2items }}"

I would prefer the "copy" approach:

\* more efficient, writes the file once  
\* your lineinfile example doesn't erase old entries with different free space value

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [January 28, 2021, 9:31am UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/11 "2021-01-28T09:31:44Z")

</div>

Hi

copy doesn’t work for me. I have 2 VG and with the copy module it looks like it rewrites the first occurrence of VG in loop, linenifile works as it should.

- name: vg free  
copy:  
dest: /tmp/file-copy  
content: “VG: {{ item.key }} free {{ item.value.free\_g }} GB”  
loop: “{{ ansible\_lvm.vgs|dict2items }}”

TASK [crate /tmp/file-copy] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [servera]

TASK [vg free] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
changed: [servera] =\> (item={‘key’: ‘apache-vg’, ‘value’: {‘size\_g’: ‘0.49’, ‘free\_g’: ‘0.12’, ‘num\_lvs’: ‘2’, ‘num\_pvs’: ‘2’}})  
changed: [servera] =\> (item={‘key’: ‘vg\_root’, ‘value’: {‘size\_g’: ‘19.00’, ‘free\_g’: ‘0’, ‘num\_lvs’: ‘2’, ‘num\_pvs’: ‘1’}})

PLAY RECAP \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
servera : ok=3 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

[student@ansiblem]$ ansible -m shell -a “cat /tmp/file-copy” servera  
servera | CHANGED | rc=0 \>\>  
VG: vg\_root free 0 GB

lineinfile:

- name: vg free  
lineinfile:  
path: /tmp/file-lineinfile  
line: “VG: {{ item.key }} free {{ item.value.free\_g }} GB”  
loop: “{{ ansible\_lvm.vgs|dict2items }}”

ansible -m shell -a “cat /tmp/file-lineinfile” servera  
servera | CHANGED | rc=0 \>\>  
VG: apache-vg free 0.12 GB  
VG: vg\_root free 0 GB

I should probably use jinja2 and template module?

Regards

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 28, 2021, 9:42am UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/12 "2021-01-28T09:42:42Z")

</div>

> Hi
> 
> copy doesn't work for me. I have 2 VG and with the copy module it looks like it rewrites the first occurrence of VG in  
> loop, linenifile works as it should.
> 
> - name: vg free  
> copy:  
> dest: /tmp/file-copy  
> content: "VG: {{ item.key }} free {{ item.value.free\_g }} GB"  
> loop: "{{ ansible\_lvm.vgs|dict2items }}"

You need to put the loop into the content.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [January 28, 2021, 10:29am UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/13 "2021-01-28T10:29:21Z")

</div>

How can I put the loop into the content? Can you please write?

Thanks

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 28, 2021, 10:45am UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/14 "2021-01-28T10:45:36Z")

</div>

> How can I put the loop into the content? Can you please write?
> 
> Thanks

Sure, here we go:

- name: vg free  
&nbsp;&nbsp;copy:  
&nbsp;&nbsp;&nbsp;&nbsp;dest: /tmp/file-copy  
&nbsp;&nbsp;&nbsp;&nbsp;content: |  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% for vg in ansible\_lvm.vgs | dict2items %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VG {{ vg.key }} {{ vg.value.free\_g }} GB  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endfor %}

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Jost](https://avatars.discourse-cdn.com/v4/letter/j/4da419/32.png) [@Jost](https://forum.ansible.com/u/Jost)\
**Post date:** [January 28, 2021, 12:49pm UTC](https://forum.ansible.com/t/report-for-mount-size-available/34494/15 "2021-01-28T12:49:37Z")

</div>

Thanks

I just add endfor ::))

copy:  
dest: /tmp/file-copy2  
content: |  
{% for vg in ansible\_lvm.vgs | dict2items %}  
VG {{ vg.key }} {{ vg.value.free\_g }} GB  
{% endfor %}
