More dynamic formulas

14 views
Skip to first unread message

Daniel Dehennin

unread,
Jun 12, 2025, 6:11:53 AMJun 12
to salt-...@googlegroups.com
Hello.

I have a formula to setup servers with additional disks, I could do it
with shell like:

#+begin_src bash
for disk in ${DISKS}
do
if configured_disk ${disk}
then
continue
fi

wipefs -a ${disk}
mkfs ${disk}
UUID=$(lsblk -n -o UUID ${disk})

echo "UUID=${uuid} /media/${UUID} ${OPTIONS} 0 0" >> /etc/fstab
mount /media/${UUID}
done
#+end_src

With salt states it's a little bit more complicated with slots and
output is ugly:

#+begin_src salt
{%- for disk, params in additionnal_disks.items() %}
{%- set mount_options = params.get("mount_options") %}
{%- set mount_user = params.get("user", "root") %}
{%- set mount_group = params.get("group", "root") %}
{%- set mount_dir_mode = params.get("dir_mode", "700") %}
{%- set mount_file_mode = params.get("file_mode", "600") %}
server/setup/disks/{{ disk }}/disk.wipe:
module.run:
- disk.wipe:
- device: {{ disk }}
- unless:
- blkid -t LABEL=salt-managed {{ disk }}

server/setup/disks/{{ disk }}/xfs.mkfs:
module.run:
- xfs.mkfs:
- device: {{ disk }}
- label: salt-managed
- ssize: size=4k
- onchanges:
- module: server/setup/disks/{{ disk }}/disk.wipe

server/setup/disks/chunk-{{ disk }}/mount.mounted:
mount.mounted:
- name: __slot__:salt:cmd.run('/bin/bash -c "echo /media/$(lsblk -n -o UUID {{ disk }})"')
- device: __slot__:salt:cmd.run("lsblk -P -o UUID {{ disk }}")
- fstype: xfs
- persist: True
- mkmnt: True
- opts: {{ mount_options }}
- dump: 0
- pass_num: {{ 2 + loop.index }}
- require:
- module: server/setup/disks/{{ disk }}/xfs.mkfs

server/setup/disks/chunk-{{ disk }}/file.directory:
file.directory:
- name: __slot__:salt:cmd.run('/bin/bash -c "echo /media/$(lsblk -n -o UUID {{ disk }})"')
- user: {{ mount_user }}
- group: {{ mount_group }}
- dir_mode: {{ mount_dir_mode }}
- file_mode: {{ mount_file_mode }}
- require:
- mount: server/setup/disks/chunk-{{ disk }}/mount.mounted

{%- endfor %}
#+end_src

I wonder if, instead of using slots, it could be more dynamic by using
the reactor or something:

1. for each disk
1. test if it's configured, next disk if OK
2. wipe the disk
3. format it
4. fire an event that disk is formatted

when handling the event, the jinja cloud then lookup information like
UUID and avoid the need for slot.

Regards.
--
Daniel Dehennin

Sid Young

unread,
Jun 15, 2025, 7:06:39 PMJun 15
to Salt-users

If you manage the with LVM then you would not need to do any of the steps you are doing as repeated re-runs would automatically check the state of the disk each time.

Just a thought.

Sid

Phipps, Thomas

unread,
Jun 16, 2025, 2:22:31 AMJun 16
to salt-...@googlegroups.com
I would also point out using labels instead of UUID for mounting would get around the issues as well. as you can setup a unique label per device. and not relying on the system creating something.

--
You received this message because you are subscribed to the Google Groups "Salt-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to salt-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/salt-users/871prpz2uj.fsf%40hati.baby-gnu.org.
Reply all
Reply to author
Forward
0 new messages