Calculate partition space used in jinja

118 views
Skip to first unread message

lift...@gmail.com

unread,
Nov 19, 2021, 3:44:06 PM11/19/21
to Ansible Project
I have a playbook that uses gather facts, then I use the ansible_mounts fact in a jinja2 template.  In this template, I can print out each partition, the total size and total available.  What I'd like to do is to also calculate the percentage of space available.  I'm trying below, but keep getting a templating error when the playbook runs.  Any ideas?

server_mounts.html.j2
<html>
<body>
Partition/Mount Space Report

{{ now(false, '%m/%d/%Y %I:%M:%S') }}

---

{% for i in play_hosts | sort %}
Host: {{ i }}

{% for m in hostvars[i]['ansible_mounts'] %}
Partition: {{ m.mount }}
Total Space: {{ m.size_total|human_readable(unit="G") }}
Available Space: {{ m.size_available|human_readable(unit="G" }}
{% if m.size_total|int != 0 %}
Space used: {{ 100 * ((m.size_total - m.size.available/m.size_available)) |round(1, 'common') }}
{% endif %}
{% endfor %}
---------------------------------------
{% endfor %}
</body>
</html>

Thanks,
Harry

Dick Visser

unread,
Nov 20, 2021, 1:10:51 AM11/20/21
to ansible...@googlegroups.com
What is the error you get ?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f2ce821e-98c6-4ae8-97bc-032580358ad6n%40googlegroups.com.
--
Sent from a mobile device - please excuse the brevity, spelling and punctuation.

lift...@gmail.com

unread,
Nov 22, 2021, 8:19:35 AM11/22/21
to Ansible Project
My apologies.  I obviously forgot to add the error.  I did find that I was missing a ')' character at the end of the "Total Space" line. But now I get the following:

FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'size'"}

The j2 file is above, but here's the playbook I'm using:

---
- hosts: localhost
  become: true
  become_method: sudo
  gather_facts: yes

  tasks:

  - name: Print out server mounts
    template:
      src: /root/server_mounts.html.j2
      dest: /var/tmp/server_mounts.html
    delegate_to: localhost
    run_once: true

I can't find why its complaining about a "size" attribute.

Thanks,
Harry

Stefan Hornburg (Racke)

unread,
Nov 22, 2021, 8:53:10 AM11/22/21
to ansible...@googlegroups.com
On 22/11/2021 14:19, lift...@gmail.com wrote:
> My apologies.  I obviously forgot to add the error.  I did find that I was missing a ')' character at the end of the "Total Space" line. But now I get the following:
>
> FAILED! => {"changed": false, "msg": "AnsibleUndefinedVariable: 'dict object' has no attribute 'size'"}
>
> The j2 file is above, but here's the playbook I'm using:
>
> ---
> - hosts: localhost
>   become: true
>   become_method: sudo
>   gather_facts: yes
>
>   tasks:
>
>   - name: Print out server mounts
>     template:
>       src: /root/server_mounts.html.j2
>       dest: /var/tmp/server_mounts.html
>     delegate_to: localhost
>     run_once: true
>
> I can't find why its complaining about a "size" attribute.

Hello Harry,

I don't see that problem either, are you really sure that the you posted the content of /root/server_mounts.html.j2?

BTW: That's a really weird location for any part of your Ansible project if you ask me.

Regards
Racke
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/f2ce821e-98c6-4ae8-97bc-032580358ad6n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/f2ce821e-98c6-4ae8-97bc-032580358ad6n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> Sent from a mobile device - please excuse the brevity, spelling and punctuation.
>
> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com <mailto:ansible-proje...@googlegroups.com>.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/4223039a-c344-4808-b72f-36adfda7f586n%40googlegroups.com <https://groups.google.com/d/msgid/ansible-project/4223039a-c344-4808-b72f-36adfda7f586n%40googlegroups.com?utm_medium=email&utm_source=footer>.


--
Ecommerce and Linux consulting + Perl and web application programming.
Debian and Sympa administration.


OpenPGP_signature
Reply all
Reply to author
Forward
0 new messages