status.diskusage in jinja

69 views
Skip to first unread message

natxo....@gmail.com

unread,
Nov 24, 2021, 10:06:15 AM11/24/21
to Salt-users
hi,

I want to get the disksize in a jinja template.

I have this:

# cat du.sls
kkkk disk usage:
  file.managed:
    - name: /tmp/kkkkk
    - source: salt://du.jinja
    - template: jinja

and in du.jinja:

# cat du.jinja
{% set du = salt['status.diskusage']() %}

{{ du }}

{% for item in du %}
{{ item }}

{% endfor %}

What I get in the file is first this:

{'/proc/sys/kernel/random/boot_id': {'available': 3219386368, 'total': 3219525632}, '/proc': {'available': 0, 'total': 0}, '/proc/asound': {'available': 0, 'total': 0}, '/proc/acpi': {'available': 0, 'total': 0}, '/proc/sys/net': {'available': 0, 'total': 0}, '/run': {'available': 3219386368, 'total': 3219525632}, '/run/host/os-release': {'available': 453957918720, 'total': 510389125120}, '/proc/fs': {'available': 0, 'total': 0}, '/run/user/0': {'available': 1609760768, 'total': 1609760768}, '/dev/shm': {'available': 1609515008, 'total': 1609764864}, '/proc/scsi': {'available': 0, 'total': 0}, '/sys/dev': {'available': 0, 'total': 0}, '/sys/kernel': {'available': 0, 'total': 0}, '/proc/sys': {'available': 0, 'total': 0}, '/sys/devices': {'available': 0, 'total': 0}, '/': {'available': 453957918720, 'total': 510389125120}, '/sys/fs/cgroup': {'available': 0, 'total': 0}, '/run/host': {'available': 3219386368, 'total': 3219525632}, '/sys': {'available': 4194304, 'total': 4194304}, '/sys/fs/selinux': {'available': 0, 'total': 0}, '/dev/mqueue': {'available': 0, 'total': 0}, '/dev': {'available': 4194304, 'total': 4194304}, '/sys/bus': {'available': 0, 'total': 0}, '/proc/kmsg': {'available': 3219386368, 'total': 3219525632}, '/tmp': {'available': 1609756672, 'total': 1609764864}, '/proc/irq': {'available': 0, 'total': 0}, '/proc/bus': {'available': 0, 'total': 0}, '/sys/block': {'available': 0, 'total': 0}, '/sys/class': {'available': 0, 'total': 0}, '/run/host/incoming': {'available': 3217514496, 'total': 3219525632}, '/dev/pts': {'available': 0, 'total': 0}}

and then I try looping and get just the mount points, and not the attributes.

If in the for loop I try {{ item.total}} to get the total size, I get:

raceback (most recent call last):
  File "/usr/lib/python3.10/site-packages/salt/utils/templates.py", line 500, in render_jinja_tmpl
    output = template.render(**decoded_context)
  File "/usr/lib/python3.10/site-packages/jinja2/environment.py", line 1304, in render
    self.environment.handle_exception()
  File "/usr/lib/python3.10/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 6, in <module>
jinja2.exceptions.UndefinedError: 'str object' has no attribute 'total'

Obviously I do something wrong, but I cannot see what.

Any ideas?

Thanks in advance.

--
regards,
Natxo

natxo....@gmail.com

unread,
Nov 24, 2021, 10:43:04 AM11/24/21
to Salt-users
hi,

found it, I needed to use the items() function in the for loop:

{% set du = salt['status.diskusage']() %}

{%- for k, v  in du.items() %}
  {%- if 'block' in k %}
  {{ k }} {{ v.available }}
  {%- endif %}
{%- endfor %}

rubberducking, thanks ;-)

--
regards,
Natxo
Reply all
Reply to author
Forward
0 new messages